Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Select Folder dialog with a difference

0.00/5 (No votes)
24 Aug 2000 2  
The Windows 'Select Folder' dialog with some extra functionality.

Sample Image - SelectFolder.gif

Introduction

The SelectFolder.zip file contains the SelectFolder.exe whose output is shown in the above picture. The "Options of Select Folder dialog" dialog shown in the above picture, shows the capabilities of the CSelectFolder class.

This Select Folder dialog was originally done by Mihai Filimon. I have added the following features -

  1. Added an edit control where the user can type in the path
  2. If the path typed in the edit control does not exist then the user will be prompted as to whether he/she wants the path to be created
  3. Setting the flag bShowFilesInDir to TRUE will result in all the files in the current folder to be displayed in the dialog
  4. If you don't want to display all the files then you can use the file filter to display the file types you want to display

Some more additional features

  1. Calling API SetTitle with the desired title will set the Title of the dialog. This API has to be invoked before DoModal is called
  2. User can pass the Initial Folder to be displayed in the constructor of CSelectFolder

Constructor

CSelectFolderDialog(BOOL bShowFilesInDir = FALSE, LPCSTR
    lpcstrInitialDir = NULL, DWORD dwFlags = OFN_HIDEREADONLY |
    OFN_OVERWRITEPROMPT,LPCTSTR lpszFilter = NULL, 
    CWnd* pParentWnd = NULL);

Usage Examples

CSelectFolderDialog oSelectFolderDialog(FALSE,
        NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,NULL, NULL);

CSelectFolderDialog oSelectFolderDialog(FALSE,
        "c:\\my documents",OFN_HIDEREADONLY |
        OFN_OVERWRITEPROMPT,NULL, NULL);

CSelectFolderDialog oSelectFolderDialog(TRUE,
        "c:\\my documents",OFN_HIDEREADONLY |
        OFN_OVERWRITEPROMPT,NULL, NULL);

CSelectFolderDialog oSelectFolderDialog(TRUE,
        "c:\\my documents",OFN_HIDEREADONLY |
        OFN_OVERWRITEPROMPT,"Microsoft Word Documents
        (*.doc)|*.doc|Microsoft Excel Worksheets
        (*.xls)|*.xls|", NULL);

CSelectFolderDialog oSelectFolderDialog(TRUE,
        "c:\\my documents",OFN_HIDEREADONLY |
        OFN_OVERWRITEPROMPT,"HTML Files (*.html,
        *.htm)|*.html;*.htm||", NULL);

Here's a complete example on how I got the select folder dialog shown in the above picture.

CSelectFolderDialog oSelectFolderDialog(TRUE,
    "C:\WINNT", 
    OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
    "Microsoft Word Documents (*.doc)|*.doc|"
    " Microsoft Excel Worksheets (*.xls)|*.xls|", 
    this);

    if (oSelectFolderDialog.DoModal() == IDOK)
        AfxMessageBox(oSelectFolderDialog.GetSelectedPath());

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here