Share via


How to: Display a List of Fonts

You can use the built-in FontDialog component to display a selection of fonts instead of creating your own font dialog box. For example, you can enable users to select a font in the dialog box, and then apply the selected font to text on the form.

To display the font dialog box

  1. On the File menu, click New Project.

    The New Project dialog box appears.

  2. Click Windows Forms Application and then click OK.

  3. From the Toolbox, drag a Label control to the form, and change the following properties in the Properties window:

    Property

    Value

    Name

    labelFont

    Text

    Sample text

  4. Add a Button control to the form, and change the following properties in the Properties window:

    Property

    Value

    Name

    setFont

    Text

    Change Font

  5. Drag a FontDialog component from the Dialogs tab of the Toolbox to the form.

    fontDialog1 appears in the component tray.

  6. Double-click the button to create the default event handler in the Code Editor.

  7. In the setFont_Click event handler, add the following code to display the font dialog box and change the font of the text in the label according to the user's choice.

    if (fontDialog1.ShowDialog() == DialogResult.OK)
    {
        this.labelFont.Font = fontDialog1.Font;
    }
    
  8. Press F5 to run the code.

  9. When the form opens, click Change font, click a font in the resulting dialog box, and then click OK.

  10. Verify that the chosen font is applied to the label's text.

  11. Close the application.

See Also

Concepts

Using Built-in Dialog Boxes in Your Application

Designing a User Interface in Visual C#

Other Resources

Dialog Boxes (Visual C#)

Visual C# Guided Tour