Click here to Skip to main content
Licence CPOL
First Posted 24 Aug 2000
Views 129,732
Bookmarked 35 times

Select Folder dialog with a difference

By | 24 Aug 2000 | Article
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Sridhar Rao

Software Developer (Senior)
Philips Electronics India Limited
India India

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questioncrash on visual studio 2010 Pinmemberkerfall3:16 1 Aug '11  
AnswerRe: crash on visual studio 2010 Pinmemberkerfall3:37 1 Aug '11  
GeneralGot an Exception when using SelectFolder dialog in visual c++ 2005 Pinmembersalimon5:55 24 Mar '10  
GeneralRe: Got an Exception when using SelectFolder dialog in visual c++ 2005 PinmemberSridhar Rao22:30 13 Apr '10  
GeneralReally useful! PinmemberDTardio3:11 25 Sep '08  
QuestionException using in VS2005 [modified] PinmemberDavid_Leikis6:26 7 Mar '07  
GeneralCan't use in a library PinmemberDavid_Leikis5:47 16 Feb '06  
GeneralBug when creating wrong folder in the Edit box PinmemberVeronique72211:13 11 Aug '05  
GeneralRe: Bug when creating wrong folder in the Edit box PinmemberRail Jon Rogut9:26 22 Nov '05  
GeneralCrashes on Windows XP PinmemberThorell1:02 25 Apr '03  
GeneralRe: Crashes on Windows XP PinmemberRail Jon Rogut21:06 6 Jul '03  
GeneralStandard dialog to browse folders PinmemberComet22:31 21 Jan '03  
GeneralRe: Standard dialog to browse folders PinsussBenny 6733:57 18 Mar '04  
GeneralDisable Create-Folder. PinsussThomas Moch3:03 14 Nov '02  
GeneralDon't work on VC7! PinmemberKondriks9:07 6 Jul '02  
Questionmultiple directory select? PinmemberJuergen Klingler2:44 15 May '02  
GeneralFit a dialog in a View of a Window PinmemberAnonymous8:47 17 Apr '02  
Questionworking without mouse -- impossible? PinmemberAnonymous18:17 14 Feb '02  
Generalnetwork name compatibility PinmemberAnonymous22:32 5 Jul '01  
Generalanother little error ... Pinmemberthom_as4:15 3 Jul '02  
Thank you guys (guess you know your names, I've forgotten them right now) for the code, and thank you, manu vanneau, for the hint, how to make the code accept network names.
There is another problem which occurs selecting network folders. Objects like the "network environment" or computers, which are displayed in the dialog like folders may be selected, but do not possess a folder name. If you doubleclick for example the "network environment" and afterwards click the select button, this causes an unhandled exception.

You can solve the problem by replacing the code within the Method OnFolderChange() by the lines


 
if(::IsWindow(GetParent()->m_hWnd))
{
CString FolderPath = GetFolderPath();
int length = FolderPath.GetLength();
if(length >0)
m_pEdit->SetWindowText(FolderPath);
}
 



If you doubleclick an invalid symbol (such as the "network environment") the funcion GetFolderPath() returns an empty string. With my few lines, I prevent the Edit Control's text to be replaced by this empty string and thus causing an exception. With the modifies code, you select the last valid folder, whose path still ist in the edit control.
 
Thomas
GeneralRe: another little error ... PinmemberAles Krajnc3:00 29 Aug '02  
GeneralSmall Error PinmemberAnonymous10:47 10 May '01  
QuestionIs this OK on Win95/98/ME ? PinsussJerry Evans8:28 4 Oct '00  
AnswerRe: Is this OK on Win95/98/ME ? PinsussSridhar20:14 4 Oct '00  
GeneralRe: Is this OK on Win95/98/ME ? PinsussJeremy Davis23:05 4 Oct '00  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 25 Aug 2000
Article Copyright 2000 by Sridhar Rao
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid