VB.Net - FontDialog Control



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 −

Sr.No. Property & Description
1

AllowSimulations

Gets or sets a value indicating whether the dialog box allows graphics device interface (GDI) font simulations.

2

AllowVectorFonts

Gets or sets a value indicating whether the dialog box allows vector font selections.

3

AllowVerticalFonts

Gets or sets a value indicating whether the dialog box displays both vertical and horizontal fonts, or only horizontal fonts.

4

Color

Gets or sets the selected font color.

5

FixedPitchOnly

Gets or sets a value indicating whether the dialog box allows only the selection of fixed-pitch fonts.

6

Font

Gets or sets the selected font.

7

FontMustExist

Gets 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.

8

MaxSize

Gets or sets the maximum point size a user can select.

9

MinSize

Gets or sets the minimum point size a user can select.

10

ScriptsOnly

Gets 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.

11

ShowApply

Gets or sets a value indicating whether the dialog box contains an Apply

button.

12

ShowColor

Gets or sets a value indicating whether the dialog box displays the color choice.

13

ShowEffects

Gets or sets a value indicating whether the dialog box contains controls that allow the user to specify strikethrough, underline, and text color options.

14

ShowHelp

Gets 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 −

Sr.No. Method Name & Description
1

Reset

Resets all options to their default values.

2

RunDialog

When overridden in a derived class, specifies a common dialog box.

3

ShowDialog

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 −

Sr.No. Event & Description
1

Apply

Occurs 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 −

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

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

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

  • 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
vb.net_dialog_boxes.htm
Advertisements