Loading, please wait...

A to Z Full Forms and Acronyms

What’s new in Windows Forms in .NET 6 | Application-wide default font for Win Form

In this post, you'll learn What’s new in Windows Forms in .NET 6 and 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:

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

application-wide default font for .NET 6 Win Form Applications

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...

A to Z Full Forms and Acronyms

Related Article

ADVERTISEMENT
ADVERTISEMENT