BlackWaspTM

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.

Font Colour Selection

If your software allows the creation of text with different colours, you can enable colour selection within the font dialog box. To show the colour options, you need to set the ShowColor property to true. The colour is held in the Color property. As with the Font property, you can set Color before showing the dialog box to specify the initial colour selection.

Modify the code for the Click event as shown below to enable colour changes. You can see that the initial colour is set to match that of the label. The label is updated only if the dialog box is closed using the OK button.

private void SelectFontButton_Click(object sender, EventArgs e)
{
    using (FontDialog dialog = new FontDialog())
    {
        dialog.ShowColor = true;
        dialog.Font = SampleLabel.Font;
        dialog.Color = SampleLabel.ForeColor;

        if (dialog.ShowDialog() == DialogResult.OK)
        {
            SampleLabel.Font = dialog.Font;
            SampleLabel.ForeColor = dialog.Color;
        }
    }
}

Limiting Font Size Options

When you open the dialog box it shows a list of preset font sizes for selection. You can also type in any other font size for more control. In some cases you might not want the user to be able to choose any size. Instead, you can limit the available sizes to a specific range. To do so, set the MaxSize and MinSize properties of the FontDialog object. Any of the preset sizes that are outside of the specified range are removed from the font size list. If the user manually enters a size that is not between the minimum and maximum values, an error message is displayed if they click OK and the dialog box remains open.

The following sample limits the available sizes. If you enter a value below twenty or above one hundred, it is not permitted.

private void SelectFontButton_Click(object sender, EventArgs e)
{
    using (FontDialog dialog = new FontDialog())
    {
        dialog.ShowColor = true;
        dialog.Font = SampleLabel.Font;
        dialog.Color = SampleLabel.ForeColor;
        dialog.MaxSize = 100;
        dialog.MinSize = 20;

        if (dialog.ShowDialog() == DialogResult.OK)
        {
            SampleLabel.Font = dialog.Font;
            SampleLabel.ForeColor = dialog.Color;
        }
    }
}

Other Configuration Options

There are a number of other Boolean options that can be set for the FontDialog. The key ones are listed below.

  • AllowScriptChange. The font dialog box includes a drop-down list that allows the user to select the character set to use when a font includes multiple character sets. When the value is false, this option is removed from the dialog box. Defaults to true.
  • AllowSimulations. Many fonts include several variations for different effects. For example, fonts might include specific versions for bold or italicised text. When a font does not include these variations, GDI simulations allow the effects to be applied. For example, a font without an italicised variant can be italicised with a GDI simulation that slants the text. Setting the AllowSimulations property to false prevents the user from selecting such simulated typefaces. Defaults to true.
  • AllowVectorFonts. Two main types of font exist. Raster fonts are created from a set of square pixels. These fonts become blocky or pixellated when increased in size. Vector fonts are created from curves, lines and filled areas so remain smooth when expanded. If you set this property to false, vector fonts will not be available for selection. Defaults to true.
  • AllowVerticalFonts. Some fonts are designed to be used vertically. These include several far Eastern fonts. If this property is set to false, these fonts are not available. Defaults to true.
  • FixedPitchOnly. Fonts can be categorised as fixed-pitch or proportional. In a fixed-pitch font, also known as fixed-width or monospaced, every character is the same width. These typefaces are useful when viewing text files, as columns of characters line up vertically. Proportional fonts have characters with differing widths and are more pleasing to the eye for normal reading. If you set this property to true, only monospaced fonts will be available for selection. Defaults to false.
  • FontMustExist. If you set this property to true, the user may only specify a typeface that is installed on the local computer. When false, it is possible to enter the name of a font that is not present. Defaults to false.
  • ShowEffects. There are several effects that you can apply to a font, including adding an underline or striking out the text. If you set this property to false, these options are removed from the dialog box. When false, the colour selection list is also hidden. Defaults to true.
13 November 2013