What’s new in Windows Forms in .NET 6 | Application-wide default font for Win Form
With .Net 6 previews 5, there is many updates and one of them you would love to know if you use to work on Win Forms Applications of the time or planning to work.
Per the official announcement by Igor with his below tweet:
Check out the new things in #winforms in .NET 6.0 Preview 5https://t.co/gH3JXEIrVm
— Igor Velikorossov (@IgorRussKie) June 23, 2021
The application-wide default font for Win Form Applications
.NET Framework and Windows Forms were designed and built in a completely different world from today – back when CRT monitors still largely maxed out at 1024×768, and “Microsoft Sans Serif” was the default font on Windows
But now you can set your custom default font application-wide in Windows Forms applications by using a new API:
void Application.SetDefaultFont(Font font)
Like many other Application
API, this API can only be used before the first window is created, which generally is in the Main()
method of the application.
class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetHighDpiMode(HighDpiMode.PerMonitorV2);
Application.SetDefaultFont(new Font(new FontFamily("Microsoft Sans Serif"), 8f));
Application.Run(new Form1());
}
}
An attempt to set the default font after the first window is created will result in an InvalidOperationException
.
As a side note, it is easy to go wild with this API, and set the application default font to ‘Chiller, 20pt, style=bold,italic,underline‘, but please be mindful of your end-user experience.
Read the full blog here by Igor...