wyDay blog  |  Downloads  |  Buy
LimeLM
wyBuild
Support forum
wyDay blog
wyDay Home

The wyDay blog is where you find all the latest news and tips about our existing products and new products to come.

wyDay blog

October 15th, 2009

This will be the quickest tip youll ever read. But first, some background. If run your .NET app on Windows Vista or Windows 7 unaltered you might notice the fonts look a little funky. Especially when compared to Microsofts own apps. See the ugly font in the window on the right:

On the left Im using the default system font in Windows Vista & 7: Segoe UI. On the right Im using the default font that every Windows Forms app uses: Microsoft Sans Serif. Its quite obvious that the Segoe UI font is slightly larger and more legible. Not only that, but if you use the font on the right instead the Microsoft Sans Serif your app will be out of place in Windows Vista & 7, since every other app in Vista & 7 uses the Segoe UI font.

Problem: Segoe UI isnt in Pre-Vista systems

The problem is that Segoe UI is only shipped with Vista & 7 versions of Windows. So you cant just hard-code the Font property of the form to be Segoe UI (or your app will look like utter crap in Windows 2000 & Windows XP).

And thus this simple snippet of code to use Segoe UI on Windows Vista & 7 while still using the default system font in Windows 98, 2000 & XP.

public Form1()
{
// use Segoe UI in Vista & 7
Font = SystemFonts.MessageBoxFont; InitializeComponent();
}

Or, in VB.NET:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' use Segoe UI in Vista & 7
Font = SystemFonts.MessageBoxFont
End Sub

Thats all you have to do to use Segoe UI font in your app. And if you set the AutoScaleMode of the form to Font then all of the controls on the form will scale to fit the larger font:

Bold font problem

What if you want to bold a title? Simple, right? Just click the Font property of the label you want to bold:

However, the problem with doing this is now every control on the form except the label you want to bold will be Segoe UI font. Why is this? Every control on your form will have the same font as the parent unless specified otherwise.

In our case the parent is the Form and were explicitly setting the font to be bold for our label to be Bold Microsoft Sans Serif.

Ok, ok but how do I make a bold label use Segoe UI?

We just learned you wont be able to do it in the designer, so instead youll have to do it in code:

public Form1()
{
// use Segoe UI in Vista & 7
Font = SystemFonts.MessageBoxFont; InitializeComponent(); // set the title to be bold
lblTitle.Font = new Font(Font, FontStyle.Bold);
}

Or in VB.NET

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' use Segoe UI in Vista & 7
Font = SystemFonts.MessageBoxFont ' set the title to be bold
lblTitle.Font = New Font(Font, FontStyle.Bold)
End Sub

7 Days of Windows 7

Join me tomorrow when I talk about adding professional looking menus to your app. See the full list of articles in the series.

Subscribe to our blog's RSS Feed or follow Wyatt (CEO of wyDay) on Mastodon (@wyatt@hachyderm.io) to keep up-to-date with our latest posts.

Comments