Common Dialog Boxes
The .NET framework provides a group of classes that can be used to show dialog boxes. These are known as the common dialog boxes. They allow you to add standard functionality for working with files, fonts, colours and printing layouts with minimal coding. The seven dialog boxes are:
- FontDialog. Allows the user to configure a font, including setting the typeface and colour, and adding effects such as bold, italics and underline.
- ColorDialog. Lets the user choose a colour using a fixed or customisable pallet.
- FolderBrowserDialog. Allows the user to browse the folders of the local file system and those of networked computers.
- OpenFileDialog. Lets the user select one or more files from the file system, with filtering options to show only specific file types.
- SaveFileDialog. Allows the user to specify the location and name for a file that is to be saved.
- PageSetupDialog. Provides configurations options for pages of printable information, including properties for the paper size, orientation and margins.
- PrintDialog. Allows the user to set printing options prior to printing their documents and data, including selecting the printer, determining the number of copies and specifying which pages to include.
In this article we'll look at the open file dialog box.
The open file dialog box is provided by the OpenFileDialog class. The dialog box shows a list of the user's folders and files, allowing the user to browse the file system and select one or more files to open. You can configure the dialog box to filter the files according to their type, which is usually determined by the file extensions. This makes it ideal when you create software that generates documents that can be opened and saved.
To demonstrate the dialog box, create a new Windows Forms project in Visual Studio. Once ready, add a ListBox and a Button to the automatically generated form. Configure the new controls as follows:
|Control Name||Type||Special Properties||Purpose|
|FileList||ListBox||none||Displays the files that the user selects in the open file dialog box.|
|OpenButton||Button||Text = "Open"||Displays the open file dialog box.|
The image below shows a possible layout for the form:
Showing the Open File Dialog Box
As with other dialog boxes, you display the dialog by calling the class's ShowDialog method. This shows the dialog in a modal manner, preventing the user from interacting with the calling window until the box is closed. The method returns a value from the DialogResult enumeration that allows you to identify the button that the user clicked. The result is OK if a file was selected, so you should check for this value before reacting to any selection. When used to select a single file, the full path for that file can be found in the FileName property.
To demonstrate, register the Click event for the button by double-clicking it. Change the code for the event as shown below. This code shows the path of the selected file in the ListBox when you click the OK button.
private void OpenButton_Click(object sender, EventArgs e)
using (OpenFileDialog ofd = new OpenFileDialog())
if (ofd.ShowDialog() == DialogResult.OK)
private void ShowFileDetails(OpenFileDialog ofd)
Try running the program and clicking the button. The dialog box should appear similar to the image below. Select a file and click the OK button to see its path. If you click the Cancel button in the dialog box, the list will not be refreshed.
23 March 2014