Dialog for Selecting (and Creating) Folders
Posted
by Nguyen Tan Hung
on May 11th, 2000
Class Purpose
The CPathDialog class lets users select or create a folder. It's just a simple extension of SHBrowseForFolder function.Functions
Class Constructor
CPathDialog::CPathDialog( LPCTSTR lpszCaption=NULL, //Caption of dialog LPCTSTR lpszTitle=NULL, //Prompt of program LPCTSTR lpszInitialPath=NULL, //Initial path CWnd* pParent=NULL //Parent of dialog );
Display Dialog
int CPathDialog::DoModal();CPathDialog returns the return value of the DoModal function (can be either IDOK or IDCANCEL).
Ensure Path Exists
int CPathDialog::MakeSurePathExists( LPCTSTR lpPath //Path to driectory you want to exist );The MakeSurePathExists function ensures that the path presented by the lpPath argument exists. If it does not exist, the function attempts to create it.
Returns of the fucntion can be:
- -1: user cancels create the lpPath directory.
- 0 : lpPath directory exists or was created.
- 1 : lpPath is invalid.
- 2 : can not create lpPath directory.
Integrating the CPathDialog into Your Project
- Import two files "pathdialog.h" and "pathdialog.cpp" into your project.
- Include the header file "pathdialog.h"
- Declare an object for CPathDialog class
- Invoke DoModal function to show dialog
- If user press OK button, get path name user's entered by using GetPathName function.
Example of Using the CPathDialog Class:
#include "PathDialog.h"
void CTestPathDialogDlg::OnBrowserButton()
{
CString strInitialPath;
CString strYourCaption(_T("Your caption here..."));
CString strYourTitle(_T("Your tilte here..."));
m_ctrlPathName.GetWindowText(strInitialPath);
CPathDialog dlg(strYourCaption, strYourTitle, strInitialPath);
if (dlg.DoModal()==IDOK)
{
m_ctrlPathName.SetWindowText(dlg.GetPathName());
}
}
void CTestPathDialogDlg::OnOKButton()
{
CString strPath;
m_ctrlPathName.GetWindowText(strPath);
if(CPathDialog::MakeSurePathExists(strPath)==0)
{
CDialog::OnOK();
}
}

Comments
Nice piece of code that really should be part of MFC anyway
Posted by Legacy on 02/18/2004 12:00amOriginally posted by: J Rees
Thanks a lot, exactly what i was looking for. Nice how it works just like the other file dialogs in mfc!
ReplyEnabling network resources
Posted by Legacy on 05/19/2003 12:00amOriginally posted by: Rob Kluver
ReplyCouldn't select the Network folder
Posted by Legacy on 03/06/2003 12:00amOriginally posted by: Simon Soosai
When i choose a local hard disk folder OK button is enabled which is right.
ReplyBut when i go to select the Network folder OK button is disabled. How do i select a network folder?
What I needed!
Posted by Legacy on 11/07/2002 12:00amOriginally posted by: Anitox
This is exactly what I needed. Thank you for your help. This code works great!
ReplyCompilation errors
Posted by Legacy on 05/20/2002 12:00amOriginally posted by: theFresh
Linking...
ReplyMyDlg.obj : error LNK2001: unresolved external symbol "public: class CString __thiscall CPathDialog::GetPathName(void)" (?GetPathName@CPathDialog@@QAE?AVCString@@XZ)
MyDlg.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall CPathDialog::DoModal(void)" (?DoModal@CPathDialog@@UAEHXZ)
MyDlg.obj : error LNK2001: unresolved external symbol "public: __thiscall CPathDialog::CPathDialog(char const *,char const *,char const *,class CWnd *)" (??0CPathDialog@@QAE@PBD00PAVCWnd@@@Z)
Release/MyApp.exe : fatal error LNK1120: 3 unresolved externals
Error executing link.exe.
Compiler error ;(
Posted by Legacy on 03/13/2002 12:00amOriginally posted by: Tutankhamen
ReplyVery nice
Posted by Legacy on 03/07/2002 12:00amOriginally posted by: Craig
Works great. You really saved me some trouble.
ReplyGreat job!!
Posted by Legacy on 03/07/2002 12:00amOriginally posted by: silver line
Thank you. great job!!
Reply
Great Code, Thanks!!!!:)
Posted by Legacy on 01/28/2002 12:00amOriginally posted by: Joe Ersinghaus
Thanks!
Replynetwork folder....disable
Posted by Legacy on 12/30/2001 12:00amOriginally posted by: Lee Gyu Seung
m.m;
i cann't it;
ReplyLoading, Please Wait ...