Home
C#.WinForms
FontDialog Example
Updated Oct 13, 2022
Dot Net Perls
FontDialog. This Windows Forms control presents a font selection dialog box. With it, we can quickly give users the ability to select their favorite fonts.
Font
DialogResult. We often use FontDialog with DialogResult. You can check the DialogResult returned by ShowDialog for the DialogResult.OK value.
DialogResult
MessageBox.Show
First, add a FontDialog instance, and also a Button and a TextBox to an empty Windows Form. Then, double-click on the Button to insert the button1_Click event handler.
Here In the example code for button1_Click, we call the ShowDialog method on the FontDialog instance.
Info The Button is linked to the button1_Click event handler. The TextBox is then changed to have the Font that was specified in the FontDialog.
TextBox
using System; using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { // Show the dialog. DialogResult result = fontDialog1.ShowDialog(); // See if OK was pressed. if (result == DialogResult.OK) { // Get Font. Font font = fontDialog1.Font; // Set TextBox properties. this.textBox1.Text = string.Format("Font: {0}", font.Name); this.textBox1.Font = font; } } } }
FixedPitchOnly. What does the FixedPitchOnly property on the FontDialog do? This changes the list of fonts in the dialog so that only the monospace ("fixed") ones are shown.
ShowColor. You can also change the ShowColor property to true—this adds a small color chooser to the FontDialog. Another option you have is the ColorDialog control, which offers more options.
ColorDialog
Tip The ShowEffects property can be set to True or False. If you set it to False, the left-hand control group will disappear.
Tip 2 If you disable ShowEffects, the color chooser drop-down will also be hidden.
Summary. The FontDialog control provides a complete font chooser. Because the dialog is built into Windows, its appearance may change depending on the version of Windows.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Oct 13, 2022 (edit link).
Home
Changes
© 2007-2025 Sam Allen