The wyDay blog is where you find all the latest news and tips about our existing products and new products to come.
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.
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:
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.
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
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.
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.
I am using Visual studio 2010 under windows 7 . When I create a in the form Designer, MS sans serif ,8.25 is taken as the defaut font.
How to tell visual studio to use Segoe Ui under W7 and MS sans serif (or Tahoma 8 ) under Windows XP ?
I can set the font at runtime,using what is written above int blog, but i would liketo design my form wirh Segoe Ui under W7 ,and with MS sans serif under Xp ..
is that possible ?
Actually setting the 'Font' property before the 'InitializeComponent();' method is a bad practice, because the 'InitializeComponent();' method will revert that to the designed value, so you muse set font AFTER 'InitializeComponent();' and NOT in 'Form.Load(...)' method because this will decrease the launching time of your software.
regards!
Since the whole point of this is to set the Font once, and to set it using the system-default font, you have to reset any "design time" Font. That way setting the Font before InitializeComponent() will work as intended (and everything will scale once).
And yes, this is the fastest way to do things.