The common file dialogs, such as File Open and File
Save, have been around since the earliest versions of Windows. The first
versions of these dialog boxes were a bit clumsy and often didn't offer
much flexibility. Configuring the dialog boxes could also be confusing
or at least difficult, but with each version of Windows, Microsoft has
sought to solve the most perplexing common file dialog problems. Windows
7 is no exception to the rule. The following sections describe the
latest version of the common file dialogs and demonstrate how you can
access them from your application using the Common File Dialogs example.
1. Considering the Common File Dialog Controls
The Microsoft.WindowsAPICodePack.Dialogs.Controls
namespace contains a number of controls you can use to interact with
the common file dialogs. In most cases, you won't need to add to or
subtract from the default configuration, but it's good to know that you
have these controls available. The following list provides a quick
overview of these controls:
CommonFileDialogButton: Creates a pushbutton control.
CommonFileDialogCheckBox: Creates a checkbox control.
CommonFileDialogComboBox: Creates a combo box control. Every combo box must have at least one CommonFileDialogComboBoxItem object in it.
CommonFileDialogComboBoxItem: Defines one ComboBoxItem for inclusion in a CommonFileDialogComboBox control.
CommonFileDialogGroupBox:
Creates a group box that can contain other controls. Grouping performs
an important function both in the visual sense (putting like
functionality together) and also in application functionality (such as
radio buttons).
CommonFileDialogLabel: Creates a label control.
CommonFileDialogMenu: Creates a menu control. Each menu control must have at least one CommonFileDialogMenuItem object in it.
CommonFileDialogMenuItem: Defines an individual menu item within a CommonFileDialogMenu object.
CommonFileDialogSeparator: Provides a separator between menu items. You use this control to create menu item groupings.
CommonFileDialogRadioButtonList: Creates a radio button list. Each radio button list must have at least one CommonFileDialogRadioButtonListItem object in it.
CommonFileDialogRadioButtonListItem: Defines an individual radio button within a CommonFileDialogRadioButtonList object.
CommonFileDialogTextBox: Creates a textbox control.
The interesting part about the
new Windows 7 common dialog boxes is that you can add any of these
controls to them and use those controls to perform special tasks (at
least within limits). For example, if users are having a hard time
understanding a particular dialog box, you can add a Help button with
additional specialized instructions.
Unfortunately, you'll find that
there are limits to what you can do with the dialog box itself. Many of
the controls are locked down once you display the dialog box. For
example, you can't modify the appearance of the dialog box after you
display it. In addition, some properties aren't defined at the time you
display the dialog box. As a result, you can't write code to clear the
file selections the user has made.
All these controls have a
complete selection of standard events. When a user clicks a button, you
can monitor the event and handle it, if desired. The standard automation
for creating event handlers also works, so you don't have to worry
about defining the proper syntax. Simply press Tab twice as you normally
would to create the event handler entries. The ability to modify the
common dialog boxes provides great flexibility, yet you still obtain all
the benefits of using a common dialog box in your application,
including a standardized appearance.
In addition to the standard controls, you also have access to abstract container controls. Here are the controls you can access:
CommonFileDialogControl: Provides an abstract class that contains all the shared functionality for the common file dialog controls.
CommonFileDialogControlCollection<T>: Creates a collection of strongly typed dialog box controls.
CommonFileDialogProminentControl: Specifies the properties and constructors for all prominent controls in common file dialogs.
2. Configuring the Common File Dialogs Example
The example begins with a Windows Forms application. The example uses one Button control named btnOpen to demonstrate the File Open common dialog and a second Button control named btnSave to demonstrate the File Save common dialog. Of course, you can use any other suitable control, such as a menu, if desired.
Because this application relies
on the Code Pack, you need to add references to it. In this case, you
need to add references to the following DLLs using the Browse tab of the
Add Reference dialog box:
Microsoft.WindowsAPICodePack.DLL
Microsoft.WindowsAPICodePack.Shell.DLL
In addition, this example requires use of the following references from the .NET tab of the Add Reference dialog box:
PresentationCore
PresentationFramework
System.Xaml
WindowsBase
Depending on the application requirements, you also need to add from one to three using statements. In most cases, it's best to add the following using statements to your application:
using Microsoft.WindowsAPICodePack.Dialogs;
using Microsoft.WindowsAPICodePack.Dialogs.Controls;
using Microsoft.WindowsAPICodePack.Shell;