Click here to Skip to main content
15,868,340 members
Articles / Desktop Programming / MFC
Article

Adding filters to the Open File dialog

Rate me:
Please Sign up or sign in to vote.
4.43/5 (3 votes)
24 Nov 19991 min read 179.1K   3.4K   27   15
Adding filters to the Open File dialog.

Sample Image

Introduction

The MFC CFileDialog class only has provision for one default file extension, which is added to a filename if no extension is given. A file export functionality deals with possibly many different file extensions, that have to be added depending on the filetype chosen. The CFileExportDialog class does this. Moreover, when the user selects another filetype from the combobox, the extension in the filename editcontrol is automatically changed.

The usage of the dialog is simple because it mimics the usage of the parent class CFileDialog. Apart from the constructor and destructor, there is only one extra public function, GetFilterIndex(), which returns the index of the selected filetype. This is useful if one extension is shared by two or more filetypes, so the filetype cannot be determined from the resulting filename. In keeping with standard Windows practice, this index starts at 1.

One thing to note is the format of the filter string. This string MUST be of the form "Word File (*.doc)|Text File (*.txt)". I.e. one extension per format, and the formats separated by pipes. Every extension must be put between parentheses and be of the form *.ext.

The initial file type is determined from the default filename, if it is given. This filename may also be "*.ext". If no default filename is given, the first filetype in the filter string is used.

The CFileExportDialog class is typically used like this:

CFileExportDialog dialog("Save As Type", // title
  "Windows Bitmap (*.bmp)|JPEG Compressed Image (*.jpg)|"\  // filter string..
  "Windows Meta File (*.wmf)|Windows Enhanced Meta File (*.emf)", // with file types
  "visualization.jpg"); // default filename (may be omitted)

if (dialog.DoModal() == IDOK) {
  CString filename = dialog.GetPathName();
  int filetype = dialog.GetFilterIndex();
  SaveFile(filename, filetype); // defined somewhere in the application
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionA Better approach Pin
beejoy19-Oct-11 2:38
beejoy19-Oct-11 2:38 
Questionvc++ Pin
veer pal singh6-Mar-07 17:51
veer pal singh6-Mar-07 17:51 
GeneralQuestion about CFile Pin
daqauto19-Dec-06 22:31
daqauto19-Dec-06 22:31 
How can I creat a dialog with 2 buttons: Open & Save; 1 ListBox. When I click on Open button, the text file I open will be displayed on the ListBox.
Could you mind giving me a sample code? Thank you very much
GeneralVS2003.NET save as dialog Pin
Michel Wassink3-Dec-06 19:58
Michel Wassink3-Dec-06 19:58 
Generalproblem with capitals Pin
Michel Wassink24-Apr-06 22:45
Michel Wassink24-Apr-06 22:45 
GeneralAdding more than one filter to same label Pin
treespleez19811-Aug-05 15:43
treespleez19811-Aug-05 15:43 
GeneralRe: Adding more than one filter to same label Pin
willbenyon23-Dec-05 0:47
willbenyon23-Dec-05 0:47 
Generalhelp Pin
Anonymous1-Jul-03 10:52
Anonymous1-Jul-03 10:52 
GeneralRe: help Pin
wduros11-Jul-03 10:57
wduros11-Jul-03 10:57 
QuestionHow about open dialog Pin
Zeurx22-Jan-03 2:38
Zeurx22-Jan-03 2:38 
AnswerRe: How about open dialog Pin
Gernot Frisch22-Jan-03 21:53
Gernot Frisch22-Jan-03 21:53 
GeneralRe: How about open dialog Pin
Zeurx23-Jan-03 16:48
Zeurx23-Jan-03 16:48 
GeneralRe: How about open dialog Pin
Gernot Frisch23-Jan-03 21:17
Gernot Frisch23-Jan-03 21:17 
GeneralRe: How about open dialog Pin
Zeurx24-Jan-03 2:29
Zeurx24-Jan-03 2:29 
GeneralRe: How about open dialog Pin
Gernot Frisch24-Jan-03 2:46
Gernot Frisch24-Jan-03 2:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.