CFileDialog Usage

CoderSource.net
CFileDialog Usage
Rating: 3

Date: 5/1/2004 12:00:00 AM

   CFileDialog MFC class can be used to invoke the Windows Open File and File Save As dialogs. This CFileDialog class encapsulates the functionalities required for retrieving file names, setting up filters, flagging over write prompts etc.,

   This CFileDialog can be used in interactive MFC GUI applications wherever we need to browse the Windows File System (and use the file selected). The applications need not worry about the gory background details of enumerating the folders, listing them in a GUI etc., All these functionalities are encapsulated within CFileDialog object.

   Once CFileDialog is instantiated and called, the user can be allowed to select the file and the programmer can use the selected file. The CFile Example explains how to manipulate files using MFC CFile class.

CFileDialog - for File Open:

   The following piece of code snippet shows how to use CFileDialog to open text files (*.txt) and comma separated value files(*.csv).


      CFileDialog l_SampleDlg(TRUE,NULL,NULL,OFN_OVERWRITEPROMPT,"Text Files (*.txt)|*.txt|Comma Separated Values(*.csv)|*.csv||");
      int iRet = l_SampleDlg.DoModal();
      CString l_strFileName;
      l_strFileName = l_SampleDlg.GetPathName();

      if(iRet == IDOK)
          MessageBox(l_strFileName);
      else
          MessageBox("No File Selected!");
 


   The above piece of code should be used in an MFC application. This will invoke the File Open dialog (constructed by using CFileDialog), with filters set for listing the *.txt and *.csv files. As seen, DoModal returns IDOK if a file is selected and Open button is clicked. Otherwise, if cancel button is clicked, it returns IDCANCEL.

   After this DoModal, calling CFileDialog ::GetPathName returns the file path. If only the file name is needed, a call to the function GetFileName will be enough.

Note:   Changing the First Parameter of CFileDialog to FALSE, this dialog will become a Save As dialog.   
 

You Can Rate this Article, if you are Logged In      
 

More Links from CoderSource.net:

 
Refer to a Friend:

Your Details:

Name:     e-mail:

Friend Details:

Name:    e-mail:    


MENU
Home
MFC 
C++
.Net
WIN32
Programming
Forum
My Articles
Add to Google
Add to My Yahoo!
Welcome to Codersource.Net Login | Register | Faq  

SEARCH
Google
 

NOTES:


Thanks for visiting our CoderSource.net. This site will be improved with more articles. Interested visitors can also submit their articles through the Submit Article link.Your article will also be published after due consideration by the editor. 

© Copyright 2003. All rights on content reserved by CoderSource.net. Contact    About Us