Wyatt Says...
This will be the quickest tip you’ll 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 Microsoft’s own apps. See the ugly font in the window on the right:

On the left I’m using the default system font in Windows Vista & 7: Segoe UI. On the right I’m using the default font that every Windows Forms app uses: Microsoft Sans Serif. It’s 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 isn’t in Pre-Vista systems
The problem is that Segoe UI is only shipped with Vista & 7 versions of Windows. So you can’t 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
That’s 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 we’re 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 won’t be able to do it in the designer, so instead you’ll 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 Wyatt Says...
Subscribe to the 'Wyatt Says...' RSS Feed and keep up to date on on my articles on updaters, usability, open source C# components, and soon anti-piracy.
Upcoming articles:
- 7 days of Windows 7: Tips, Tricks, and Controls to get your C# and .NET apps Windows 7 ready October 14th - October 22nd
- Quick C# Tip: Adding animation to your Windows Forms app
- Building a Great Updater Part 2: Adobe Updater, Inconsistency is Thy Name
Subscribe now, and don't miss out.
Or subscribe to Wyatt Says... by Email
Hi,
I’m looking forward to the other tips as I’m curious about some of them. However, this tip is not enough for proper font support in Windows Forms.
I think that a better approach like this is much better:
http://texhex.blogspot.com/2008/02/setting-right-font-for-windows-forms.html
Thanks Ricardo. I’d always considered MS Sans Serif close enough to Tahoma that it didn’t matter all that much. But I realize not everyone agrees.
Thanks, that’s a good resource.
I think you missed the point.
That blog post is not about MS Sans Serif vs Tahoma. It’s about having the proper font in every Form of the application, consistent with the OS the user is running. No matter the size, style or whatever. It’s about fixing a bug the best possible way that Microsoft overlooked.
Oh no, I didn’t miss the point. I was just commenting on the MS Sans Serif vs. Tahoma bit of the code. I saw the rest of it and I thought it was a bit overkill.
For example, the “Fix” function loops over every control on the form. This is death for large projects. If we used this function with wyBuild it would add an extra 10 seconds to the startup time.
That’s why I was proposing leaving the Font properties of form “unset” so they will inherit the MessageBoxFont from the parent Form. Then apply styling to only the controls that need it.
It’s a question of developer time vs. added run time.
But the autoscale thing is problem IMO.
If you set it to “Font”, you’ll have different control sizes depending on the OS the user is running their application and while you’re builiding your app on a specific OS, some user running it on a different OS maybe be presented with a really weird interface.
Ah, I see. Yes, you’re right, some control scale oddly. Multi-line textboxes, rich text boxes, and list boxes are three that don’t scale proportionally.
It’s too bad Microsoft is letting the Windows Forms space languish while they try to attract programmers to the utterly slow Windows Presentation Foundation.
This is all well and good but you forgot to mention that Segoe UI is not a freeware font. It is under a commercial license, so you’re not able to distribute it freely with your installer.. meaning it will not be displayed on machines that do not already have it installed. Just because Microsoft bought the license doesn’t mean you get to use it for free ;-)
Here’s the link for Microsoft’s Page for this font
http://www.microsoft.com/typography/Fonts/family.aspx?FID=331
And here’s the link to the company who wrote it, Ascender
http://www.ascendercorp.com/font/segoe-ui/
That’s true Bill, but I wasn’t suggesting using Segoe UI with Pre-Vista versions of Windows. This article was about using the default system font as the font for your forms (rather than hardcoding the font type, or using Tahoma for all versions of Windows).
There’s a fairly simple solution to the autoscale issue. The reason that some controls don’t autoscale properly is a side effect of the fact that the form’s properties are set in alphabetical order in the designer file created by the Visual Studio forms designer. Open the designer file and move the line that sets the Autoscale = Font to below any Controls.Add lines and you should find that your controls now scale correctly.
Unfortunately you’ll need to re-apply the fix if you change the form as the designer file will be re-written with the code back in the original order.
nice article man, thanks for sharing your idea
We absolutely love your blog and find almost all of your post’s to be just what I’m looking for. Does one offer guest writers to write content for you personally? I wouldn’t mind composing a post or elaborating on a few of the subjects you write concerning here. Again, awesome web log!
Ascender’s Fold is different from Microsoft Fonts.