[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Use file dialog filters in C#

[Use file dialog filters in C#]

The OpenFileDialog and SaveFileDialog components have a Filter property that lets the user look for specific kinds of files. The property's value should be a pipe symbol (|) separated list of alternating filter names and patterns. Each pattern can include more than one file matching pattern separated by semi-colons.
This example uses the following Filter (all on one line):

Bitmaps|*.bmp| PNG files|*.png| JPEG files|*.jpg| Picture files|*.bmp;*.jpg;*.gif;*.png;*.tif| CS files|*.cs| Project files|*.csproj| Program files|*.cs;*.csproj;*.sln;*.suo;*.resx

The parts of the filter are:

  • Bitmaps - *.bmp
  • PNG files - *.png
  • JPEG files - *.jpg
  • Picture files - *.bmp;*.jpg;*.gif;*.png;*.tif
  • CS files - *.cs
  • Project files - *.csproj
  • Program files - *.cs;*.csproj;*.sln;*.suo;*.resx

For example, if the user selects the "Picture files" filter, the dialog lists files with the extensions .bmp, .jpg, .gif, .png, and .tif.

The file selection dialogs also have a FilterIndex property that indicates the currently selected filter. You can set this to initially select a filter. Note, however, that the first filter's index is 1 instead of the 0 as is usually the case for arrays in .NET.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.