VB.Net - FontDialog Control

Advertisements


It prompts the user to choose a font from among those installed on the local computer and lets the user select the font, font size, and color. It returns the Font and Color objects.

Following is the Font dialog box:

VB.Net Font Dialog

By default, the Color ComboBox is not shown on the Font dialog box. You should set the ShowColor property of the FontDialog control to be True.

Properties of the FontDialog Control

The following are some of the commonly used properties of the FontDialog control:

S.NPropertyDescription
1AllowSimulationsGets or sets a value indicating whether the dialog box allows graphics device interface (GDI) font simulations.
2AllowVectorFontsGets or sets a value indicating whether the dialog box allows vector font selections.
3AllowVerticalFontsGets or sets a value indicating whether the dialog box displays both vertical and horizontal fonts, or only horizontal fonts.
4ColorGets or sets the selected font color.
5FixedPitchOnlyGets or sets a value indicating whether the dialog box allows only the selection of fixed-pitch fonts.
6FontGets or sets the selected font.
7FontMustExistGets or sets a value indicating whether the dialog box specifies an error condition if the user attempts to select a font or style that does not exist.
8MaxSizeGets or sets the maximum point size a user can select.
9MinSizeGets or sets the minimum point size a user can select.
10ScriptsOnlyGets or sets a value indicating whether the dialog box allows selection of fonts for all non-OEM and Symbol character sets, as well as the ANSI character set.
11ShowApplyGets or sets a value indicating whether the dialog box contains an Apply button.
12ShowColorGets or sets a value indicating whether the dialog box displays the color choice.
13ShowEffectsGets or sets a value indicating whether the dialog box contains controls that allow the user to specify strikethrough, underline, and text color options.
14ShowHelpGets or sets a value indicating whether the dialog box displays a Help button.

Methods of the FontDialog Control

The following are some of the commonly used methods of the FontDialog control:

S.NMethod Name & Description
1Reset
Resets all options to their default values.
2RunDialog
When overridden in a derived class, specifies a common dialog box.
3ShowDialog
Runs a common dialog box with a default owner.

Events of the FontDialog Control

The following are some of the commonly used events of the FontDialog control:

S.NEventDescription
1ApplyOccurs when the Apply button on the font dialog box is clicked.

Example

In this example, let's change the font and color of the text from a rich text control using the Font dialog box. Take the following steps:

  1. Drag and drop a RichTextBox control, a Button control and a FontDialog control on the form.

  2. Set the Text property of the button control to 'Change Font'.

  3. Set the ShowColor property of the FontDialog control to True.

  4. Double-click the Change Color button and modify the code of the Click event:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
   If FontDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
      RichTextBox1.ForeColor = FontDialog1.Color
      RichTextBox1.Font = FontDialog1.Font
   End If
End Sub

When the application is compiled and run using Start button available at the Microsoft Visual Studio tool bar, it will show the following window:

VB.Net Font dialog Example

Enter some text and Click on the Change Font button.

VB.Net Font Dialog Example

The Font dialog appears, select a font and a color and click the OK button. The selected font and color will be applied as the font and fore color of the text of the rich text box.

VB.Net Font Dialog Example

Advertisements
Advertisements