C#Dot Net Perls

C#
Segoe UI and Tahoma in Windows Forms

by Sam Allen
Segoe UI in Windows Forms.

Problem

Adjust our programs to use Segoe UI on Windows Vista and Tahoma on Windows XP. Using one font on both XP and Vista isn't ideal, and we want to switch between Tahoma and Segoe UI. Using Segoe UI on XP doesn't display Segoe--it displays a somewhat ugly sans serif font. We need a way to make the font look perfect on systems running Vista and XP.

C# Solution

The solution to the font inconsistency problem is to set the font in the form constructor dynamically. You will need to get the value of the MessageBoxFonts on the SystemFonts static class and apply it as the default font. The controls in the form will inherit this font, so you won't need to set each on individually.

// (This is your Form constructor.)
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();
    }
}

Conclusion

Enhance the user experience for your programs on both Windows XP and Vista. Set Tahoma and Segoe in your forms. This will make the program look really classy on both operating systems. I highly recommend adding this simple line of code. It can really make a difference.

Dot Net Perls is dedicated to sharing code and knowledge. It has
© 2007-2008 Sam Allen. All rights reserved.

Ads by The Lounge