The Font Dialog Box



To assist the user with selecting a font for an application, Microsoft Windows provides the Font dialog box:


The Font dialog box appears with three combo boxes of a simple style, a group box that contains a label, a combo box of a drop down list style, and two buttons (labeled OK and Cancel).

The list of fonts installed on the computer appear in the Font combo box. To select a font, the user can start typing its name and the combo box would present suggested names. The user can also scroll down in the list and click the desired font.

The four types of font styles are listed in the Font Style combo box: the Regular, the Italic, the Bold, and the combined Bold Italic styles.

The possible sizes are listed in the Size combo box. The user can click a size or scroll down in the list to find the desired size. The user is allowed to use a size that is not listed. To use it, the user can click in the text box side of the control, delete the number, and type the new number.

As the user is making the selections that would define a font, the Sample label shows a preview of the selection.

The Script combo box allows the user to specify an alphabetic category. The options are Western (the default for a US-English computer), Greek, Turkish, Central European, and Cyrillic.

After making the selections in the Font, the Font Style, and the Size combo boxes, the user can click OK.

As an option, the Font dialog box can allow the user to apply two more styles and a color to the selected font. To make this possible, the lower-left section of the dialog box can be equipped with an Effects group box. Here is an example:

The Font Dialog Box With Styles and Color

The Effects group box is equipped with a Strikeout check box, an Underline check box, and a Color combo box. To apply a style, the user can click its check box. To remove a style, the user can uncheck it. To select a color, the user can click the arrow of the Color combo box and select a color.

As mentioned already, after making the selections, the user can click OK. To dismiss the selections, the user can click Cancel or press Esc. After clicking OK or Cancel, the dialog box would be closed. As an alternative, the Font dialog box can be equipped with an Apply button. In this case, when the dialog box comes up, the user can make selections. If the user clicks Apply, the new selections would be executed, as if the user had clicked OK, but the dialog box would continue displaying. After clicking Apply, even if the user clicks Cancel, the changes made on the dialog box would be validated.

Creating a Font Dialog Box

In the .NET Framework, the Font dialog box is represented by the FontDialog class. The FontDialog class is derived from the CommonDialog class.

At design time, to provide a Font dialog to your application, from the Dialogs section of the Toolbox, you can click the FontDialog button FontDialog and click the form. To programmatically provide a Font dialog box to your application, declare a variable of type FontDialog and use the new operator to allocate its memory.

To display a Font dialog box to the user, call its ShowDialog() method. Here is an example:

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms

Module Exercise

    Public Class Starter
        Inherits Form

        Private dlgFont As FontDialog
        Dim components As System.ComponentModel.Container

        Public Sub New()
        End Sub

        Public Sub InitializeComponent()

            dlgFont = New FontDialog()

        End Sub

    End Class

    Function Main() As Integer

        Dim frmStart As Starter = New Starter


        Return 0
    End Function

End Module

This would produce:

The default Font dialog box of the .NET Framework


Home Copyright © 2008-2016, FunctionX, Inc. Next