Showing a Font List with the FontDialog Component
Collapse the table of content
Expand the table of content
The document is archived and information here might be outdated

Showing a Font List with the FontDialog Component

Visual Studio .NET 2003

The FontDialog component allows users to select a font, as well as change its display aspects, such as its weight and size.

The font selected in the dialog box is returned in the Font property. Thus, taking advantage of the font selected by the user is as easy as reading a property.

To select font properties using the FontDialog Component

  1. Display the dialog box using the ShowDialog method.
  2. Use the DialogResult property to determine how the dialog box was closed.
  3. Use the Font property to set the desired font.

    In the example below, the Button control's Click event handler opens a FontDialog component. When a font is chosen and the user clicks OK, the Font property of a TextBox control that is on the form is set to the chosen font. The example assumes your form has a Button control, a TextBox control, and a FontDialog component.

    ' Visual Basic
    Private Sub Button1_Click(ByVal sender As System.Object, _
       ByVal e As System.EventArgs) Handles Button1.Click
       If FontDialog1.ShowDialog() = DialogResult.OK Then
          TextBox1.Font = FontDialog1.Font
       End If
    End Sub
    // C#
    private void button1_Click(object sender, System.EventArgs e)
       if(fontDialog1.ShowDialog() == DialogResult.OK)
          textBox1.Font = fontDialog1.Font;
    // C++
       System::Void button1_Click(System::Object *  sender,
          System::EventArgs *  e)
          if(fontDialog1->ShowDialog() == DialogResult::OK)
             textBox1->Font = fontDialog1->Font;
    Visual C# and C++ Note   Be sure that the necessary code to enable the event handler is present. In this case, it would be similar to the following:
    // C#
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // C++
    button1->Click += new System::EventHandler(this, button1_Click);

See Also

FontDialog Component (Windows Forms) | FontDialog Class

© 2016 Microsoft