Click here to Skip to main content
15,508,644 members
Articles / Desktop Programming / Windows Forms
Article
Posted 31 Jul 2007

Stats

32.1K views
352 downloads
12 bookmarked

File Dialog Filter Builder

Rate me:
Please Sign up or sign in to vote.
2.64/5 (4 votes)
31 Jul 2007CPOL
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:

C#
// 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)


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

Comments and Discussions

 
GeneralOrdering and Default Pin
Member 334239818-Aug-09 13:25
Member 334239818-Aug-09 13:25 
GeneralSystem extension description Pin
Chris Richner1-Aug-07 3:06
Chris Richner1-Aug-07 3:06 
GeneralRe: System extension description Pin
Jakub Mller1-Aug-07 3:44
Jakub Mller1-Aug-07 3:44 
It could be useful, but how resolve application name for filter item with *.wmv, *.avi, *.mpeg, when different application will be assigned for every file type?

Geniality is in simplicity.

AnswerRe: System extension description Pin
Chris Richner1-Aug-07 13:26
Chris Richner1-Aug-07 13:26 
GeneralRe: System extension description Pin
Anthony Daly27-Oct-09 10:46
Anthony Daly27-Oct-09 10:46 
Generalspelling mistake Pin
Michael Sync31-Jul-07 23:27
Michael Sync31-Jul-07 23:27 
GeneralRe: spelling mistake Pin
Jakub Mller31-Jul-07 23:45
Jakub Mller31-Jul-07 23:45 

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.