C# Segoe UI, Tahoma in Windows Forms

by Sam Allen - Updated November 27, 2009
Segoe UI

You want to adjust your program to use Segoe UI on Windows Vista and Tahoma on Windows XP. Using one font on both XP and Vista isn't ideal. Using Segoe UI on XP doesn't display Segoe; it displays an ugly sans-serif font. Here we look at how you can change the font to Segoe UI in Windows Forms.

Using SystemFonts

Set the font in the form constructor dynamically. You will need to get MessageBoxFonts on the SystemFonts enumeration and apply it as the default font. The controls in the form will inherit this font.

public partial class Form1 : Form
{
    public Form1()
    {
        //
        // Set form's font to MessageBoxFont.
        // All controls in the form will inherit it.
        //
        this.Font = SystemFonts.MessageBoxFont;
        InitializeComponent();
    }
}

It adds complexity. Some font information in the Visual Studio Designer might be lost. For a control to inherit the MessageBoxFont set in the example, it shouldn't specify any font in the Designer.

You should change AutoScaleMode. Remember to set the AutoScaleMode property to None. Otherwise, the window will change dimensions unpredictably because Vista uses a 9 point font size, and Windows XP uses an 8 point font size.

It isn't slow. Remember the way that the runtime constructs Windows Forms. If you set a font after the layout is complete, then Windows will need to re-measure, re-flow, and re-layout the controls.

Set it before InitializeComponent. Set the font in your constructor before the call to InitializeComponent. This makes some windows are more than 10% faster to display. With the code before InitializeComponent, there is no performance loss.

Summary

Here we saw how you can enhance the user experience for your programs on both Windows XP and Vista. Set Tahoma and Segoe in your Windows Forms. This will make the program look really classy on both operating systems. This simple line of code can really make a difference.

(Do not copy this page.)

Dot Net Perls | Search
Windows Forms | BackgroundWorker Tutorial | DataGridView Tips and Secrets | DataGridView Tutorial | MessageBox.Show Examples in Windows Forms | TextBox Tutorial
C# | Integer.TryParse | ArrayList Examples | Bituminous Coal | Sleep Method Use
© 2009 Sam Allen. All rights reserved.
Dot Net Perls | Sam Allen