Click here to Skip to main content
14,739,420 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have created an MFC SDI project based on the explorer style (it has a left-hand pane containing a tree-view, and a right-hand pane into which I wish to open files).

There is already a file-open menu item which opens a file dialog. When the user selects a file from within the file-open dialog, how do I know that a file has been selected and what its name and directory location is? How do I capture this in my code?

Many thanks.
Posted

It's been more than four years since I wrote any MFC stuff, but it goes something like this:

CString path;
if (dlg.DoModal() == IDOK)
{
    path = dlg.GetPathName();
}
   
Comments
Albert Holguin 24-Mar-11 14:54pm
   
that's pretty good memory...lol
Jackie Lloyd 25-Mar-11 4:34am
   
Hi, thankyou very much for your reply - it is very much appreciated.

The main problem I have is that I don't know what the instance of CFileDialog is called. I cannot find any refernce to CFileDialog in the project code, and there is no CFileDialog in the resource view (although there is clearly the dialog for the 'About' dialog). However, when I run the app, the file->open option on the menu-bar clearly opens the file dialog and allows me to select a file (altho nothing happens when I click OK). I do not know how to 'get hold' of the CFileDialog object. Sorry if i did not make that clear initially.
Hi You can use the Return value of CFileDialog::DoModa(), Function. If it is IDOK the user selected a file and closed the dialog by clicking on open button and if it is IDCANCEL the dialog was closed using cancel button or X button.
Example:

CFileDialog FileDilog;
if(FileDialog.DoModal() == IDOK)
{
      //Full path of selected file.
      CString strFilePath = FileDialog.GetPathName();
}


In case if you want to know allow multiple file selection Use The code,
if (FileDialog.DoModal() == IDOK)
{
  POSITION pos = FileDialog.GetStartPosition();
  while (pos != NULL)
  {
    CString strFilePath = FileDialog.GetNextPathName(pos);
    // Process the file
  }
}


Check MSDN for More info.
http://msdn.microsoft.com/en-us/library/8dwz4azc%28v=vs.80%29.aspx[^]
Comments
Jackie Lloyd 25-Mar-11 4:34am
   
Hi, thankyou very much for your reply - it is very much appreciated.

The main problem I have is that I don't know what the instance of CFileDialog is called. I cannot find any refernce to CFileDialog in the project code, and there is no CFileDialog in the resource view (although there is clearly the dialog for the 'About' dialog). However, when I run the app, the file->open option on the menu-bar clearly opens the file dialog and allows me to select a file (altho nothing happens when I click OK). I do not know how to 'get hold' of the CFileDialog object. Sorry if i did not make that clear initially.
Just override the virtual OnFileOpen function in your application's CDocument based class.

virtual BOOL OnOpenDocument(LPCTSTR lpszPathName);
   
Comments
Ozer Karaagac 24-Mar-11 22:38pm
   
This one is the more appropriate answer within Document/View architecture.
Jackie Lloyd 25-Mar-11 4:36am
   
Hi - thankyou very much for your reply. I do not know how to get at the instance of CFileDialog as I cannot find what it's name is anywhere in my application.
Ozer Karaagac 25-Mar-11 8:39am
   
In doc/view, you normally don't need to handle CFileDialog yourself. Framework does this for you and send you selected file name by lpszPathName parameter of above function when you override CYourAppDoc::OnOpenDocument() member.

If you don't want to use Doc/View architecture and want to show CFileDilog yourself, you need to handle ID_FILE_OPEN event in one of your Doc, View, Frame or App derived classes.
Dear Jackie,
I have beel searching all through internet for the same application like treeview display by opening a file. As i am newbie to this technology im not sure how to proceed further so can u please share ur project with me it ll be more helpfull for my career. Can u please mail it to my email id mithunssss3@gmail.com.
Thanks in Advance
   
Comments
J.Surjith Kumar 22-Mar-13 10:17am
   
Why you posted this as the solution???

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




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900