Common Dialog Boxes
The .NET framework includes classes that define a set of seven common dialog boxes. These are dialog boxes that provide users with a standardised way to interact with files, select fonts and colours, and define printing layouts. They are useful because they provide a familiar user interface with very little coding. The 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 save file dialog box.
The save file dialog box is defined in the SaveFileDialog class. The dialog box allows the user to browse the file system to find the folder where they wish to save their data. It also provides a text box where they enter the name of the file to be created or updated, or they can select a file to be overwritten. You can configure the dialog box to filter the files according to one or more file extensions, making it very useful for software that allows for the creation of documents that can be opened and saved.
To demonstrate the dialog box, create a new Windows Forms project using Visual Studio. Once the project is initialised, add a TextBox and a Button to the default form. Configure the two controls as shown in the table below:
|Control Name||Type||Special Properties||Purpose|
|TextInput||TextBox||AcceptsReturn = True|
Multiline = True
|Allows the input of some text to be saved.|
|SaveButton||Button||Text = "Save"||Displays the save file dialog box.|
The image below shows a possible layout for the form:
Showing the Save File Dialog Box
As with the other common dialog boxes, to display the save dialog you create a new instance of the class and call its ShowDialog method. The resultant dialog box is modal, meaning that the window that opened it becomes unresponsive to input until the pop-up is closed. The ShowDialog method returns a value from the DialogResult enumeration that tells you which button the user clicked. You should only perform the save action if this value is DialogResult.OK.
To demonstrate, switch to the code behind the window and add the following using directive. This will simplify access to the File class, which we will use to generate text files.
Next, register the Click event by switching back to the designer and double-clicking the button. Change the code for the button click to that shown below. This writes the contents of the text box to a file when you provide a path and filename and click the dialog box's correct button. Note that the selected path is found in the dialog box object's FileName property.
private void SaveButton_Click(object sender, EventArgs e)
using (SaveFileDialog sfd = new SaveFileDialog())
if (sfd.ShowDialog() == DialogResult.OK)
private void SaveFile(SaveFileDialog sfd)
string path = sfd.FileName;
Try running the program, entering some text into the text box and clicking the Save button. The save dialog box should appear:
Select a folder to store a new file and enter a name into the File name box. For simplicity, use the .txt extension for the file so that Windows can identify it as a text file. Click the Save or OK button to complete the operation. The file will be created.
6 May 2014