Click here to Skip to main content
Licence CPOL
First Posted 31 Jul 2007
Views 15,573
Downloads 133
Bookmarked 11 times

File Dialog Filter Builder

By | 31 Jul 2007 | Article
Helper class for building filter string for Windows file dialogs

Introduction

The Windows file dialogs provide the Filter property to specify which kinds of files should be shown in the dialog. The syntax is pretty simple, but there is still scope to make a mistake and "create" a bug (with typo for example).

Using the Code

The FileDialogFilterBuilder class uses the FilterInfo structure for every filter item. The FilterInfo structure contains all information about one filter item. Using these two types are pretty simple and intuitive:

// Create builder
FileDialogFilterBuilder filterBuilder = new FileDialogFilterBuilder();
// add filter item for Word documents
filterBuilder.Infos.Add( new FilterInfo( "Word", "doc", "docx", "rtf" ) );
// add filter item for Excel documents
filterBuilder.Infos.Add( new FilterInfo( "Excel", "xls", "xlsx", "csv" ) );

// create filter item for images
FilterInfo infoImages = new FilterInfo( "Images" );
// these extensions will be used by dialog for filtering files
infoImages.Extensions = new string[] 
	{ "bmp", "jpg", "gif", "jpeg", "png", "wmf", "emf", "ico" };
// these extensions will be displayed to user with item
infoImages.VisibleExtensions = new string[] { "bmp", "jpg", "gif" };
// add item to builder
filterBuilder.Infos.Add( infoImages );

// add item with no-filter, with title "All file types"
filterBuilder.AddAllFileTypes( "All file types" );

using ( OpenFileDialog ofd = new OpenFileDialog() ) {
    // method "ToFilterString()" builds the filter string with correct syntax
    ofd.Filter = filterBuilder.ToFilterString();
    ofd.ShowDialog( this );
} 

That's all.

History

  • 1st August, 2007: Initial post

License

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

About the Author

Jakub Mller

Web Developer

Czech Republic Czech Republic

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
GeneralOrdering and Default PinmemberMember 334239812:25 18 Aug '09  
GeneralSystem extension description PinmemberChris Richner2:06 1 Aug '07  
GeneralRe: System extension description PinmemberJakub Müller2:44 1 Aug '07  
AnswerRe: System extension description PinmemberChris Richner12:26 1 Aug '07  
GeneralRe: System extension description PinmemberAnt21009:46 27 Oct '09  
Generalspelling mistake PinmemberMichael Sync22:27 31 Jul '07  
GeneralRe: spelling mistake PinmemberJakub Müller22:45 31 Jul '07  

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
Web04 | 2.5.120517.1 | Last Updated 1 Aug 2007
Article Copyright 2007 by Jakub Mller
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid