Let the User Pick a Font with FontDialog in C#

FontDialog shows the standard Windows font picker and hands you back a Font object. It is part of Windows Forms, so it is Windows-only: the project needs net8.0-windows and UseWindowsForms, which is what dotnet new winforms gives you:

<PropertyGroup>
  <OutputType>WinExe</OutputType>
  <TargetFramework>net8.0-windows</TargetFramework>
  <UseWindowsForms>true</UseWindowsForms>
  <Nullable>enable</Nullable>
  <ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

Basic usage

This handler lets the user change the font of a text box:

private void FontButton_Click(object? sender, EventArgs e)
{
    using var dialog = new FontDialog
    {
        Font = notesTextBox.Font,
        FontMustExist = true
    };

    if (dialog.ShowDialog(this) == DialogResult.OK)
    {
        notesTextBox.Font = dialog.Font;
    }
}

A few details that the short version of this example usually skips:

  • Preselect the current font. Setting Font before showing the dialog opens it with that face, style and size selected. Without it the user starts from the default font every time.
  • Pass the owner. ShowDialog(this) makes the dialog modal to your form and keeps it on top of it. Without an owner it is attached to whatever window happens to be active.
  • Dispose the dialog. Like every common dialog, FontDialog is a component that implements IDisposable. using var takes care of it when the method ends.
  • Check the result. ShowDialog returns DialogResult.Cancel when the user closes the dialog or presses Escape. Only read Font after OK.

Let the user pick a color too

The dialog can show a color drop-down next to the effects (strikeout and underline):

using var dialog = new FontDialog
{
    Font = notesTextBox.Font,
    ShowColor = true,
    Color = notesTextBox.ForeColor
};

if (dialog.ShowDialog(this) == DialogResult.OK)
{
    notesTextBox.Font = dialog.Font;
    notesTextBox.ForeColor = dialog.Color;
}

The color list is the same 16 basic colors Windows has offered since the 1990s. If you need a full color picker, use a separate ColorDialog.

Live preview with the Apply button

Set ShowApply = true and the dialog gets an Apply button that raises the Apply event without closing the dialog. That makes a cheap preview, as long as you restore the original font when the user cancels:

private void FontButton_Click(object? sender, EventArgs e)
{
    Font originalFont = notesTextBox.Font;

    using var dialog = new FontDialog
    {
        Font = originalFont,
        ShowApply = true
    };

    dialog.Apply += (_, _) => notesTextBox.Font = dialog.Font;

    notesTextBox.Font = dialog.ShowDialog(this) == DialogResult.OK
        ? dialog.Font
        : originalFont;
}

Limiting the choice

These properties cut down the list or the size range:

PropertyEffect
MinSize, MaxSizeLimit the sizes the user can pick, in points. 0 means no limit.
FixedPitchOnlyOnly monospaced fonts, useful for code editors and terminals.
ShowEffectsShow or hide strikeout, underline and the color list. On by default.
AllowVerticalFontsInclude fonts whose names start with @, used for vertical East Asian text.

Saving the choice

A Font is not something to store directly. To remember the user’s choice between runs, save the family name, size and style and rebuild the font on startup:

var font = new Font(savedFamily, savedSize, savedStyle);

If the font was uninstalled in the meantime, GDI+ silently substitutes a default family rather than throwing, so compare font.Name with the saved name if it matters.

Font is IDisposable. Fonts you create yourself and later replace should be disposed; the ones you get from controls or from the dialog are fine to leave to the garbage collector in a typical app.

Related examples

Leave a Comment