Click here to Skip to main content
11,637,415 members (57,438 online)
Click here to Skip to main content

SelectDialog - A Multiple File and Folder Select Dialog

, 26 Dec 2008 CPOL 66.6K 4.5K 33
Rate this:
Please Sign up or sign in to vote.
A multi select files and folders browse window
SelectDlg.jpg

Introduction

What's a Problem?

It has probably happened that you need a dialog like CFileDialog (MFC) that allows you to select some items together. I do not mean multi select file feature but multi select files and folders.

If you search MSDN and the internet for libraries which do that for you, maybe the best things you would find are CFileDialog for selecting single or multiple files and SHBrowseForFolder API for selecting a single folder.
So the problem is that you need a utility that makes it possible to select every browsable thing.

What Should We Do To Overcome the Problem?

If you want to have a single class using it you can select a file or a folder or some files or some folders or a mixture of them. I searched for such a utility but found nothing. So I decided to provide it and the best solution that I found was changing CFileDialog to do what I need.

Solution

CSelectDialog is a class that is inherited from CFileDialog and makes it possible for you to browse your computer or network and select a mixture of files and folders. For my convenience (as implementer) and user, I add a member of type CStringArray to the class to keep selected items after dialog returns IDOK.

Difference of CSelectDialog and CFileDialog

  1. Their user interface has some differences (You can see it in sample image in Vista.)
  2. OnInitDone, OnFolderChange, OnFileNameOK and WndProc functions of CFileDialog are overridden in CSelectDialog.

Using the Code

Use of CSelectDialog is similar to CFileDialog. Simply define a variable of type CSelectDialog and DoModal it. After user clicks the OK button, you can access the full path of selected items via m_lstSelectedItems member. A code snippet that does this work is shown below:

    CSelectDialog ofd(TRUE, _T("*.*"), NULL,
            OFN_HIDEREADONLY | OFN_ALLOWMULTISELECT,
            _T("All files and folders(*.*)|*.*||") );

    if( ofd.DoModal() != IDOK )
        return;

    for( int i=0; i<ofd.m_SelectedItemList.GetCount(); i++ )
        m_lstSelectedItems.AddString( ofd.m_SelectedItemList[i] );

Some Notes

  1. If you want to disable multi select, you should make a change in CSelectDialog constructor.

  2. If you want to hide a combobox and its label below select dialog, you should uncomment 2 lines below in OnInitDone function:

        ...
        //HideControl(cmb13);
        //HideControl(stc3);
        ...
  3. For prevention of exception in XP SP3, you should comment all lines containing cmb13 in OnInitDone function.

Points of Interest

When I searched the Internet, nobody provided such a browse dialog with this feature.

History

  • Version 1.0 July 2008: First version of SelectDialog

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Hojjat Bohlooli
Other
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
حجت اله بهلولی
متولد 18 بهمن 1360 اصفهان
مدرک کارشناسی ارشد مهندسی کامپیوتر - نرم افزار

Hojjat Bohlooli
I was born in 7 Feb 1982, Isfahan Iran
Bachelor of science in Computer Science from Yazd university
Master of science in Software Engineering from university of Isfahan

You may also be interested in...

Comments and Discussions

 
GeneralTo Support Only Folder selection Pin
Subramaniam Natarajan14-May-15 22:03
memberSubramaniam Natarajan14-May-15 22:03 
QuestionAccess violation writing location 0x00000020 Pin
Member 941911216-Sep-14 17:51
memberMember 941911216-Sep-14 17:51 
AnswerRe: Access violation writing location 0x00000020 Pin
Member 1126396625-Nov-14 12:11
memberMember 1126396625-Nov-14 12:11 
BugFail to grab the Directory path sometimes Pin
Member 1084033124-Jun-14 11:08
memberMember 1084033124-Jun-14 11:08 
GeneralRe: Fail to grab the Directory path sometimes Pin
Member 1126396630-Nov-14 16:33
memberMember 1126396630-Nov-14 16:33 
GeneralMy vote of 5 Pin
Hojjat Bohlooli28-Jul-12 3:28
memberHojjat Bohlooli28-Jul-12 3:28 
Questionhow to use it in vc6.0 Pin
xunonxyz22-Apr-12 1:16
memberxunonxyz22-Apr-12 1:16 
Generalcompiles using Visual Studio 2010 on Windows Server 2008 64-bit Pin
RedDK23-May-11 7:50
memberRedDK23-May-11 7:50 
QuestionWindows 7 and VS2008 - not working !? Pin
Miguelit021-Feb-10 22:56
memberMiguelit021-Feb-10 22:56 
AnswerRe: Windows 7 and VS2008 - not working !? Pin
Nic Wilson30-Jun-10 13:40
memberNic Wilson30-Jun-10 13:40 
GeneralRe: Windows 7 and VS2008 - not working !? Pin
Hojjat Bohlooli1-Jul-10 2:55
memberHojjat Bohlooli1-Jul-10 2:55 
GeneralRe: Windows 7 and VS2008 - not working !? Pin
Member 1084033126-Jun-14 7:44
memberMember 1084033126-Jun-14 7:44 
AnswerRe: Windows 7 and VS2008 - not working !? Pin
Hojjat Bohlooli28-Jul-12 4:10
memberHojjat Bohlooli28-Jul-12 4:10 
As mentioned by some users OnInitDone function does not works in Win7 so we should add two new parameters at the end of constructor as shown below:

CSelectDialog(
BOOL bOpenFileDialog,
LPCTSTR lpszDefExt,
LPCTSTR lpszFileName,
DWORD dwFlags,
LPCTSTR lpszFilter,
CWnd* pParentWnd)
:CFileDialog(
bOpenFileDialog,
lpszDefExt,
lpszFileName,
dwFlags | OFN_EXPLORER | OFN_HIDEREADONLY &(~OFN_SHOWHELP),
lpszFilter,
pParentWnd,
0, FALSE) // two new parameters tdisable vista style

GeneralRe: Windows 7 and VS2008 - not working !? Pin
Miguelit013-Aug-12 11:42
memberMiguelit013-Aug-12 11:42 
QuestionWould this be possible in C#? Pin
Tony Horray1-Feb-10 16:07
memberTony Horray1-Feb-10 16:07 
AnswerRe: Would this be possible in C#? Pin
Member 77366443-Apr-11 11:12
memberMember 77366443-Apr-11 11:12 
GeneralWhy is the last selected file stored at address zero (in the m_lstSelectedItems[0] ) Pin
jtchangvit10-Nov-09 3:53
memberjtchangvit10-Nov-09 3:53 
GeneralRe: Why is the last selected file stored at address zero (in the m_lstSelectedItems[0] ) Pin
Hojjat Bohlooli12-Nov-09 4:02
memberHojjat Bohlooli12-Nov-09 4:02 
GeneralRe: Why is the last selected file stored at address zero (in the m_lstSelectedItems[0] ) Pin
Hojjat Bohlooli15-Nov-09 2:01
memberHojjat Bohlooli15-Nov-09 2:01 
GeneralBug with hidden file extensions Pin
paste20009-Apr-09 2:07
memberpaste20009-Apr-09 2:07 
GeneralVS2008 and Vista Pin
pavritch12-Jan-09 19:48
memberpavritch12-Jan-09 19:48 
GeneralRe: VS2008 and Vista Pin
Nagender2-May-09 2:30
memberNagender2-May-09 2:30 
GeneralRe: VS2008 and Vista Pin
mark gooding18-Sep-09 0:34
membermark gooding18-Sep-09 0:34 
AnswerRe: VS2008 and Vista Pin
Miguelit011-Aug-10 14:45
memberMiguelit011-Aug-10 14:45 
GeneralMy vote of 1 Pin
Rob Graham26-Dec-08 8:49
memberRob Graham26-Dec-08 8:49 
GeneralRe: My vote of 1 Pin
Johannes P. Hansen30-Jun-09 22:13
memberJohannes P. Hansen30-Jun-09 22:13 
GeneralRe: My vote of 1 Pin
Hojjat Bohlooli1-Jul-09 2:51
memberHojjat Bohlooli1-Jul-09 2:51 
GeneralRe: My vote of 1 Pin
Johannes P. Hansen2-Jul-09 2:01
memberJohannes P. Hansen2-Jul-09 2:01 
GeneralRe: My vote of 1 Pin
Hojjat Bohlooli2-Jul-09 2:59
memberHojjat Bohlooli2-Jul-09 2:59 
GeneralRe: My vote of 1 Pin
Johannes P. Hansen2-Jul-09 3:21
memberJohannes P. Hansen2-Jul-09 3:21 
GeneralRe: My vote of 1 Pin
Hojjat Bohlooli3-Jul-09 8:08
memberHojjat Bohlooli3-Jul-09 8:08 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150728.1 | Last Updated 26 Dec 2008
Article Copyright 2008 by Hojjat Bohlooli
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid