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:
FileDialogFilterBuilder filterBuilder = new FileDialogFilterBuilder();
filterBuilder.Infos.Add( new FilterInfo( "Word", "doc", "docx", "rtf" ) );
filterBuilder.Infos.Add( new FilterInfo( "Excel", "xls", "xlsx", "csv" ) );
FilterInfo infoImages = new FilterInfo( "Images" );
infoImages.Extensions = new string[]
{ "bmp", "jpg", "gif", "jpeg", "png", "wmf", "emf", "ico" };
infoImages.VisibleExtensions = new string[] { "bmp", "jpg", "gif" };
filterBuilder.Infos.Add( infoImages );
filterBuilder.AddAllFileTypes( "All file types" );
using ( OpenFileDialog ofd = new OpenFileDialog() ) {
ofd.Filter = filterBuilder.ToFilterString();
ofd.ShowDialog( this );
}
That's all.
History
- 1st August, 2007: Initial post