1

I'm trying to utilize one of CFileDialog's parameter OFN_ALLOWMULTISELECT to allow user to select multiple file paths with ctrl+click. However, it doesn't fulfill my goal which is to also make selections from other folders without opening and closing the dialog again. I've searched the internet for some solutions but seems like that's just how OFN_ALLOWMULTISELECT works.

This code below is what works fine, but only for selecting within the same folder:

CFileDialog fileDialog( TRUE, NULL, NULL,
        /*OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_EXPLORER |*/ OFN_ALLOWMULTISELECT,
        NULL, this);
if (fileDialog.DoModal() == IDOK)
{
    //Multi-selection
    CString strPaths;

    CComPtr<IFileOpenDialog> piod = fileDialog.GetIFileOpenDialog();
    ASSERT( piod );

    CComPtr<IShellItemArray> pResults;
    if( SUCCEEDED( piod->GetResults( &pResults ) ) )
    {
        DWORD count = 0; pResults->GetCount( &count );
        for( DWORD i = 0; i < count; ++i )
        {
            CComPtr<IShellItem> pItem;
            if( SUCCEEDED( pResults->GetItemAt( i, &pItem ) ) )
            {
                CComHeapPtr<wchar_t> pPath;
                if( SUCCEEDED( pItem->GetDisplayName( SIGDN_FILESYSPATH, &pPath ) ) )
                {
                    if( !strPaths.IsEmpty() )
                        strPaths += L"\n";
                    strPaths += pPath;
                }
            }
        }
    }

    m_strAddedFilePaths = strPaths;
    //UpdateData(FALSE);
}

[Graphical explanation]:

For example, I select two files here. enter image description here

As I navigate to a different folder and select a new file, the previous selection would be gone enter image description here

So the question is how do I retain my previous selections?

Is this considered a limitation in this class or is there a way to do it?

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Sin Han Jinn
  • 574
  • 3
  • 18
  • 1
    Interesting question (don't know the answer, though). Hope you don't mind the minor edit I made. :) – Adrian Mole Dec 28 '22 at 09:37
  • 3
    That's not supported. You can have multiple selections within a single folder, but when navigating to a different folder, the selection is reset, and the user has to start over. If your use-case were possible, how would a user keep track of the files they had already selected while navigating to a new folder? – IInspectable Dec 28 '22 at 20:25
  • @IInspectable Just some background, so basically I'm creating an application which helps developers upload multiple files to the server. And these files will definitely be scattered at different directories. So right after they finish selecting, I'm still listing their selection with more information such as "is it modified?", "newly added" and their version numbers. That's when they can review their selections. If I've got no other choice, I've to probably prompt this dialog multiple times while waiting on an input on whether to "complete" or "continue with selection". – Sin Han Jinn Dec 28 '22 at 23:38
  • @AdrianMole not a problem! – Sin Han Jinn Dec 28 '22 at 23:39
  • I don't think I ever seen an application behaving like this! Perhaps there are better approaches to do it. – sergiol Dec 29 '22 at 10:07
  • IMHO you want a simple explorer style window on the right and a drop list on the right or vice versa). And user drags the files they want to process into this list. When they are ready they hit the button. to do whatever it is you want to do. – Andrew Truckle Dec 30 '22 at 14:20

1 Answers1

1

You can customize CFileDialog as described here: MFC extending CFileDialog by adding a list control where you would accumulate all the selected files from different folders. You can add your "is it modified?", "newly added", etc. there, too.

Vlad Feinstein
  • 10,960
  • 1
  • 12
  • 27