The FontDialog component displays the dialog box shown in Figure G-7 to let the user select a font. If the user selects a font and clicks OK, the ShowDialog method returns DialogResult.OK.
Figure G-7: The FontDialog component lets the user select a font.
The component has several properties that determine the options that the dialog displays and the types of fonts it will allow the user to select. The following table describes some of the most important of the component’s properties.
Property | Purpose |
---|---|
AllowScriptChange | Determines whether the component allows the user to change the character set shown in the Script combo box (Western in Figure G-7). If this is False, the drop-down is still visible, but it only contains one choice. |
AllowSimulations | Determines whether the component allows graphics device font simulations. For example, many fonts do not include bold or italics, but the graphics device interface (GDI) can simulate them. |
AllowVectorFonts | Determines whether the component allows vector fonts. (Characters in a raster font are drawn as bitmaps. Characters in a vector font are drawn as a series of lines and curves. Vector fonts may provide a nicer look because they scale more easily than raster fonts, but vector fonts may take longer to draw.) |
AllowVerticalFonts | Determines whether the component allows vertical fonts (such as Chinese). |
Color | Sets or gets the font’s color. Note that this is not part of the Font property, so if you want to let the user set a control’s font and color, you must handle them separately, as shown in the following code:
dlgFont.Font = Me.Font dlgFont.Color = Me.ForeColor dlgFont.ShowColor = True If dlgFont.ShowDialog() = DialogResult.OK Then Me.Font = dlgFont.Font Me.ForeColor = dlgFont.Color End If |
FixedPitchOnly | Determines whether the dialog box only allows fixed-pitch (fixed-width) fonts. (In a fixed-width font, all characters have the same width. For example, this sentence is in the Courier font, which is fixed- width. In a variable-width font, some characters such as l and i are thinner than other characters such as W. This sentence is in the Times New Roman font, which has variable width. |
Font | Sets or gets the font described by the dialog box. |
FontMustExist | Determines whether the component raises an error if the user tries to select a font that doesn’t exist. |
MaxSize | Determines the maximum allowed point size. |
MinSize | Determines the minimum allowed point size. |
ShowApply | Determines whether the dialog box displays the Apply button. If you set this to True, you should catch the component’s Apply event (as shown in the code example later in this section). |
ShowColor | Determines whether the component allows the user to select a color. |
ShowEffects | Determines whether the component displays the Effects group box that includes the Strikeout and Underline boxes and the Color drop-down. |
ShowHelp | Determines whether the dialog box displays the Help button. If you set this to True, you should catch the component’s HelpRequest event. The event handler might explain to the user how the font will be used. For example, “Select the font to be used for item descriptions.” |
The following code shows how a program might use the dialog box to let the user set a form’s font. The btnSetFont_Click event handler runs when the user clicks the program’s Set Font button. The code saves a copy of the form’s current font and foreground color. It sets the dialog box’s ShowColor and ShowEffects properties to True so the user can select the font’s color, Strikeout, and Underline values. It sets ShowApply to True to make the dialog display its Apply button.
Next, the program calls the dialog box’s ShowDialog method. If ShowDialog returns DialogResult.OK, then the user selected a font and clicked OK. In that case, the program sets the form’s font by calling subroutine SetFormFont, passing it the dialog box’s current font and color.
If ShowDialog returns DialogResult.Cancel, then the user clicked Cancel. In that case, the program calls subroutine SetFormFont, passing it the form’s original saved font and color to restore those values.
If the user clicks the dialog’s Apply button, then the dlgFont_Apply event handler executes. That routine calls subroutine SetFormFont, passing it the dialog box’s current font and color to apply the current values.
Subroutine SetFormFont simply sets the form’s font and foreground color.
' Let the user pick a new font for the form. Private Sub btnSetFont_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnSetFont.Click ' Save the current font and color. Dim old_font As Font = Me.Font Dim old_color As Color = Me.ForeColor dlgFont.ShowColor = True ' Let the user select font color. dlgFont.ShowEffects = True ' Let the user select Strikeout and Underline. dlgFont.ShowApply = True ' Display the Apply button. dlgFont.Font = Me.Font ' Set the initial font. ' Display the dialog. If dlgFont.ShowDialog() = DialogResult.OK Then ' The user clicked OK. Apply the new font. SetFormFont(dlgFont.Font, dlgFont.Color) Else ' The user canceled. Restore the old font. SetFormFont(old_font, old_color) End If End Sub ' Apply the dialog's currently selected font. Private Sub dlgFont_Apply(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles dlgFont.Apply Dim dlg As FontDialog = DirectCast(sender, FontDialog) SetFormFont(dlg.Font, dlg.Color) End Sub ' Set the form's Font and ForeColor. Private Sub SetFormFont(ByVal new_font As Font, ByVal new_color As Color) ' Set the form's font and color. Me.Font = new_font Me.ForeColor = new_color End Sub