Class to select directory (Enhancements)


This is a small enhancement of the CDirDialog class contributed by Girish Bharadwaj and Lars Klose.
I have extended the CDirDialog class with the following useful features

  • You can specify the parent window of the dialog, so it can act as a modal dialog.
  • You can specify the title of the dialog window
  • The DoBrowse() method truncates the '\' character of the default directory.
  • The class has a virtual member that can dynamically enable and disable the OK button. This virtual member can also specify the text that will be displayed in the status area of the dialog box.
New members of my CDirDialog class are
CString m_strWindowTitle
Specifies the dialog window title.
BOOL m_bStatus
Specifies whether the dialog has status text or not.
virtual BOOL SelChanged(LPCSTR lpcsSelection, CString& csStatusText)
This virtual method is called each time the selection changes. If SelChanged() returns TRUE then the OK button is enabled, otherwise it is disabled. If the m_bStatus flag is set it may also return the text that will be displayed in the status area of the dialog box. This text is returned in the second parameter - csStatusText. The default implementation simply returns TRUE.

I have also changed the DoBrowse() member. It's syntax now is
DoBrowse(CWnd *pwndParent = NULL)
You can specify the parent window here. If you do so the dialog will act as a modal dialog.

As to SelChanged virtual method, one can consider that a pointer to function would be more convenient to use. This is the matter of taste. I think that generally speaking virtual function is safer and more obvious.

That's all. Just in case I'll remind how to use the class.

Set the title by setting the text in m_strTitle. (Why do they call this title? I don't know; I should say it's a simple static text above the directories list; but I'll follow the MS terminology) If you don't set this the text will be "Open".

Set the dialog window title in m_strWindowTitle.

Set the root directory using m_strInitDir. By default it would be desktop.

Set the default directory in m_strSelDir. If you don't set this, the default directory will be the root directory.

Then call DoBrowse. If it returns TRUE, you can see the m_strPath for the selected directory. If it returns FALSE, user has cancelled the dialog or there was some problem retrieving the folder. If you define pwndParent parameter of DoBrowse the dialog would be modal, otherwise it would not.

If you want to enable and disable the OK button dynamically, write descendant class with your own SelChanged method.

Download source - 2 KB


  • It work! Very good

    Posted by Legacy on 01/10/2004 12:00am

    Originally posted by: Leonardo

    It was the unique class that works correctly in this section for classes that open a directory... Great!

  • Very good class

    Posted by Legacy on 04/30/2002 12:00am

    Originally posted by: Oktawian Szczurek

    After adding m_strSelDir to current version this class is very usefull.
    Well done!

    Best regards,

  • What about creating paths?

    Posted by Legacy on 01/21/1999 12:00am

    Originally posted by: Worik

    This is a good class. But it would be useful if the user could
    create a new path.

    I tried addidng...


    which according to the documentation should have added an edit box,
    but it did not.


  • Loading a bitmap into it

    Posted by Legacy on 01/10/1999 12:00am

    Originally posted by: Ronald

    Hello there,

    Is there anyway i could load a bitmap into the background of this

    If it is going to be difficult, where could i get another sample
    which uses MFC to implement?


Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Moving from an on-premises environment to Office 365 does not remove the need to plan for disruptions or reduce the business risk requirements for protecting email services. If anything, some risks increase with a move to the cloud. Read how to ease the transition every business faces if considering or already migrating to cloud email. This white paper discusses: Setting expectations when migrating to Office 365 Understanding the implications of relying solely on Exchange Online security Necessary archiving …

  • Anthony Christie, the Chief Marketing Officer for Level Communications, is responsible for customer experience, worldwide marketing and product management. In this informative asset, he shares his insights into why a private network connection to cloud-bases applications is the right decision for your enterprise. Download now to find out more.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date