Class to select directory (Enhancements) | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Sep 1, 1998
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

.

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&amp 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

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.