Partilhar via


How to: Show a Font List with the FontDialog Component

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.

    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
    
    private void button1_Click(object sender, System.EventArgs e)
    {
       if(fontDialog1.ShowDialog() == DialogResult.OK)
       {
          textBox1.Font = fontDialog1.Font;
       }
    }
    
    private void button1_Click(Object sender, System.EventArgs e)
    {
       if (fontDialog1.ShowDialog() == DialogResult.OK)
       {
          textBox1.set_Font(fontDialog1.get_Font());
       }
    }
    
    private:
       void button1_Click(System::Object ^ sender,
          System::EventArgs ^ e)
       {
          if(fontDialog1->ShowDialog() == DialogResult::OK)
          {
             textBox1->Font = fontDialog1->Font;
          }
       }
    

    (Visual C#, Visual J#, and Visual C++) Place the following code in the form's constructor to register the event handler.

    this.button1.Click += new System.EventHandler(this.button1_Click);
    
    this.button1.add_Click(new System.EventHandler(this.button1_Click));
    
    button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
    

See Also

Reference

FontDialog Class

Other Resources

FontDialog Component (Windows Forms)