This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Windows Programming
.NET 1.1+

The Font Selection Dialog Box

The .NET framework class library includes several common dialog boxes. These allow standard interactions with the user, such as font, file or colour selection or configuration for printing. This article describes the font selection dialog box.

Common Dialog Boxes

In order to standardise certain interactions with the user, and to lessen the amount of work for developers, the .NET framework includes a number of common dialog boxes. These are familiar windows that can be displayed to the user in order to request information. There are seven common dialogs that you can utilise, rather than implementing your own versions:

  • 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 font selection dialog box.

FontDialog Class

The FontDialog class provides the common dialog box that permits a user to configure a font. The dialog box includes various options for changing the typeface used, the size and colour of text, and effects such as bold, italics, underline and strikeout. As a developer, you can use various properties of the class to limit the user's font options so that they are in line with the requirements of your project.

To demonstrate the dialog box we need a sample project. Create a new Windows Forms project. Once the solution is initialised, open the designer for the automatically generated form. Add a button and a label to the form, using the properties described in the following table.

Control NameTypeSpecial PropertiesPurpose
SelectFontButtonButtonText = "Select Font"Displays the font selection dialog box when clicked.
SampleLabelLabelAutoSize = True
Text = "This is some sample text."
Shows a sample of the font selected using the font selection dialog box.

For the best results, place the label below the button. This will prevent the label from obscuring the button if the text size is increased. If you download the sample code using the link at the start of this article, you'll see a form layout similar to that shown below:

Sample Layout for FontDialog Demo Form

Showing the Font Selection Dialog Box

You can display a basic font selection dialog box simply by creating an instance of the FontDialog type and calling its ShowDialog method. The dialog box is shown as a modal window, preventing the user from further interacting with the window that launched it until the dialog box is closed. This also means that control is not passed to the statement following ShowDialog whilst the dialog remains visible.

The user can close the dialog box by clicking either the OK or Cancel button, or by closing the window using standard window controls. You can determine which button was clicked using the ShowDialog method's return value. This will be a value from the DialogResult enumeration. You should check that the value is DialogResult.OK before applying any of the user's selections, as they would not expect to see any changes if they click Cancel.

The new typeface options are held in the Font property of the dialog box object. This returns a Font instance, which can be applied directly to many controls that display text.

To demonstrate, double-click the button in the form designer to add an empty Click event. Modify the generated method as shown below. This code starts by creating a FontDialog instance and setting the Font property to the font from the label. This sets up the dialog box to match the label's current typeface options. The dialog box is then opened.

If the user clicks the OK button within the dialog box, the new font options are stored in the label's font, updating the sample. Note that the operation is contained within the scope of a using statement. FontDialog implements IDisposable, so the statement ensures that the dialog box object is correctly disposed.

private void SelectFontButton_Click(object sender, EventArgs e)
    using (FontDialog dialog = new FontDialog())
        dialog.Font = SampleLabel.Font;

        if (dialog.ShowDialog() == DialogResult.OK)
            SampleLabel.Font = dialog.Font;

Try running the program. When you click the button the basic font dialog box will be displayed:

Basic FontDialog

Try changing the various font options. You should see that the new font is correctly applied to the label when you click OK. If you update the font options and click the Cancel button the label remains unchanged.

13 November 2013