Click here to Skip to main content
Licence 
First Posted 24 Nov 1999
Views 129,146
Bookmarked 26 times

Adding filters to the Open File dialog

By | 24 Nov 1999 | Article
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

About the Author

Anneke Sicherer-Roetman

Web Developer

Netherlands Netherlands

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionA Better approach Pinmemberbeejoy2:38 19 Oct '11  
Questionvc++ PinmemberMember #384309717:51 6 Mar '07  
GeneralQuestion about CFile Pinmemberdaqauto22:31 19 Dec '06  
GeneralVS2003.NET save as dialog PinmemberMichel Wassink19:58 3 Dec '06  
Generalproblem with capitals PinmemberMichel Wassink22:45 24 Apr '06  
GeneralAdding more than one filter to same label Pinmembertreespleez198115:43 1 Aug '05  
GeneralRe: Adding more than one filter to same label PinPopularmemberwillbenyon0:47 23 Dec '05  
Generalhelp PinsussAnonymous10:52 1 Jul '03  
GeneralRe: help Pinmemberwduros110:57 1 Jul '03  
QuestionHow about open dialog PinmemberZeurx2:38 22 Jan '03  
AnswerRe: How about open dialog PinmemberGernot Frisch21:53 22 Jan '03  
GeneralRe: How about open dialog PinmemberZeurx16:48 23 Jan '03  
GeneralRe: How about open dialog PinmemberGernot Frisch21:17 23 Jan '03  
GeneralRe: How about open dialog PinmemberZeurx2:29 24 Jan '03  
GeneralRe: How about open dialog PinmemberGernot Frisch2:46 24 Jan '03  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 25 Nov 1999
Article Copyright 1999 by Anneke Sicherer-Roetman
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid