0

C++/MFC, Windows 11. Visual Studio 2022 17.4.3.

I'm using CFileDialog to allow user to chose a file. I want to be notified whenever the user changes directories (folders), but I can't get OnFolderChange to work. I expect OnFolderChange() to be called every time user selects a folder, but it is not called.

What do I need to do to get the notification when user switches folders?

Here is sample code.

Header:

// Browsing_test.h

class P
{
public:
    int BrowseTest(void);
    virtual void OnFolderChange();
};
#include "Browsing_test.h"

int P::BrowseTest(void)
{
    DWORD flags = OFN_CREATEPROMPT | OFN_HIDEREADONLY | OFN_ENABLESIZING;
    CFileDialog dlg(TRUE, NULL/*defaultExt*/, NULL/*defaultFiles*/, flags, NULL/*fileTypes*/, nullptr/* pParentWnd*/, 0/*dwSize*/, TRUE/*bVistaStyle*/);
    int resultDebug = (int)dlg.DoModal();
    return resultDebug;
}

void P::OnFolderChange()
{
    bool stopHere = true; // NEVER GETS HERE
}
Woody20
  • 791
  • 11
  • 30
  • 5
    The [documentation](https://learn.microsoft.com/en-us/cpp/mfc/reference/cfiledialog-class#onfolderchange) points out: *"Notification is sent only if the dialog box was created with the OFN_EXPLORER style."* While I don't know whether this is correct, there's also a fundamental issue with your code: `CFileDialog` doesn't know about your `P` class. You'd have to derive `P` from `CFileDialog` to receive `OnFolderChange` notifications in your override. – IInspectable Jan 05 '23 at 20:37
  • `OFN_EXPLORER` is automatically added to the flags for Vista-style. I will try your suggestion of deriving `P` from `CFileDialog `. – Woody20 Jan 05 '23 at 20:49
  • You should be able to effect this with ClassWizard, which will add message map entries for you. – Den-Jason Jan 05 '23 at 23:17
  • 4
    @Den-Jason no message map is needed for this. Just derive a class from `CFileDialog` and override [`OnFolderChange`](https://learn.microsoft.com/en-us/cpp/mfc/reference/cfiledialog-class?view=msvc-170#onfolderchange), which will automatically be called. – Jabberwocky Jan 06 '23 at 09:43
  • @Jabberwocky: Your suggestion of a derived class worked. – Woody20 Jan 10 '23 at 18:39

0 Answers0