Solved

CFileDialog

Posted on 2006-07-06
5
1,345 Views
Last Modified: 2013-11-20
I want to be able to select a folder on my HD and put the path into a text field. This should be easy enough except I can't get the CFileDialog to allow me to just select a folder. It needs me to select a file. I assume there's a flag that specifies you don't need to select a file, but I can't figure out what the flag is. I've checked msdn and can't see it. I have done it easily enough before with Win32, but can't seem to get CFileDialog to do it.

I tried creating the dialog as follows:
CFileDialog SourceFile(TRUE, NULL, NULL, !OFN_FILEMUSTEXIST);

All this does is specify that the file doesn't have to be there. This way, I can navigate to the folder, type in any randon gibberish, and when the dialog closes, extract the folder path. This is a little messy and would rather be able to just select the folder.

Any ideas what I'm doing wrong?
0
Comment
Question by:krispin
5 Comments
 
LVL 5

Accepted Solution

by:
bastibartel earned 30 total points
ID: 17054787
Hi there,

You cannot specify !OFN_FILEMUSTEXIST as a flag to CFileDialog::CFileDialog ()
The flags cannot be negated.

CFileDialog as it is does not allow folder selection.

Check this out (EE):
http://www.experts-exchange.com/Programming/Programming_Languages/MFC/Q_10244185.html

or (Codeproject)
http://www.codeproject.com/dialog/XFolderDialog.asp?df=100&forumid=163247&exp=0&select=1073456

Cheers,
Sebastian
0
 
LVL 22

Assisted Solution

by:mahesh1402
mahesh1402 earned 20 total points
ID: 17056323
>>I want to be able to select a folder on my HD and put the path into a text field.

Any reason you dont want to use 'Browse For Folder' Dialog using SHBrowseForFolder() ?
http://www.codechamber.com/tutorials/cpptechniques/browseforfolder.htm

Otherwise look at this 'XFolderDialog - a folder selection dialog based on CFileDialog'
http://www.codeproject.com/dialog/XFolderDialog.asp

-MAHESH


0
 
LVL 22

Expert Comment

by:mahesh1402
ID: 17056327
OOPs ....not looked.....XFolderDialog already given by bastibartel ..

0
 
LVL 7

Expert Comment

by:nafis_devlpr
ID: 17080147
what you can do is to derive a class of your own using the CFileDialog class as base class. Then in the header file of the class you just created just replace the constructor with the following.

YourClass::YourClass(BOOL bOpenFileDialog, LPCTSTR lpszDefExt, LPCTSTR lpszFileName,
            DWORD dwFlags, LPCTSTR lpszFilter, CWnd* pParentWnd) :
            CFileDialog(bOpenFileDialog, lpszDefExt, "Don't Type Anything here", dwFlags, lpszFilter, pParentWnd)
{
}


then override the virtual function "OnFileNameOK()" of CFileDialog and give the following body to this function

BOOL YourClass::OnFileNameOK()
{
      return 0;
}


and thats it, now whenever u need the dialog invoke it in this way

YourClass m_fdFilePath(FALSE, NULL, "", OFN_HIDEREADONLY, NULL);

but u will have to parse the folder path which can be done in the following way

int i;
CString strm_fdFilePath.GetPathName()
i=str.ReverseFind('\\')+1;
if(i > 0)
      str.SetAt(i, '\0');

and presto, u will get the folder path u have just selected.

Nafis
0
 
LVL 2

Expert Comment

by:snyp
ID: 17185578
you can browse for a folder using about 4 lines of code;

void MyApp::OnButton1() {

char folderName[512];
BROWSEINFO browseInfo = { 0 };

LPITEMIDLIST itemIdList = SHBrowseForFolder(&browseInfo);
SHGetPathFromIDList(itemIdList, folderName);

if (strlen(folderName)) {
// a folder was selected and it's path is here
}
}
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Add content to output file 4 62
substring method in java 1 97
sum28 challenge 31 106
Bartender label printing - switch on and off graphics 3 10
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now