What’s new in Windows Forms in .NET 6.0

Igor

We continue to support and innovate in Windows Forms runtime. Let’s recap what we’ve done in .NET 6.0.

Accessibility improvements and fixes

Accessibility improvements, What’s new in Windows Forms in .NET 6.0

Making Windows Forms applications more accessible to more users is one of the big goals for the team. Building on the momentum we gained in .NET 5.0 timeframe in this release we delivered further improvements, including but not limited to the following:

  • Improved support for assistive technology when using Windows Forms apps. UIA providers enable tools like Narrator and others to interact with the elements of an application. UIA is also often used to create test automation to drive apps. We have now added UIA providers support for the following controls:

    • CheckedListBox
    • LinkLabel
    • Panel
    • ScrollBar
    • TabControl
    • TrackBar
  • Improved Narrator announcements in DataGridView, ErrorProvider and ListView column header controls.
  • Keyboard tooltips for the TabControl’s TabPage and the TreeView’s TreeNode controls.
  • ScrollItem Control Pattern support for ComboBoxItemAccessibleObject.
  • Corrected control types for better support of Text Control Patterns.
  • ExpandCollapse Control Pattern support for the DateTimePicker control.
  • Invoke Control Pattern support for the UpDownButtons component in DomainUpDown and NumericUpDown controls.
  • Improved color contrast in the following controls:
    • CheckedListBox
    • DataGridView
    • Label
    • PropertyGridView
    • ToolStripButton

 

Application bootstrap

In .NET Core 3.0 we started to modernize and rejuvenate Windows Forms. As part of that initiative we changed the default font to Segoe UI, 9f (dotnet/winforms#656), and quickly learned that a great number of things depended on this default font metrics. For example, the designer was no longer a true WYSIWYG, as Visual Studio process is run under .NET Framework 4.7.2 and uses the old default font (Microsoft Sans Serif, 8.25f), and .NET application at runtime uses the new font. This change also made it harder for some customers to migrate their large applications with pixel-perfect layouts. Whilst we had provided migration strategies, applying those across hundreds of forms and controls could be a significant undertaking.

To make it easier to migrate those pixel-perfect apps we introduced a new API (for more details refer to the Application-wide default font post):

void Application.SetDefaultFont(Font font)

However, this API wasn’t sufficient to address the designer’s ability to render forms and controls with the same new font. At the same time, with our sister teams heavily pushing for little code/low ceremony application templates, our Program.cs and its Main() method started looking very dated, and we decided to follow the general .NET trend and trim the boilerplate. Please welcome the new Windows Forms application bootstrap:

class Program
{
    [STAThread]
    static void Main()
    {
        ApplicationConfiguration.Initialize();
        Application.Run(new Form1());
    }
}

ApplicationConfiguration.Initialize() is a source generated API that behind the scenes emits the following calls:

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.SetDefaultFont(new Font(...));
Application.SetHighDpiMode(HighDpiMode.SystemAware);

The parameters of these calls are configurable via MSBuild properties in csproj or props files. The Windows Forms designer in Visual Studio 2022 is also aware of these properties (for now it only reads the default font), and can show you your application (C#, .NET 6.0 and above) as it would look at runtime:

Application bootstrap. What’s new in Windows Forms in .NET 6.0

(We know, the form in the designer still has that Windows 7 look, We’re working on it…)

Please note that Visual Basic handles these application-wide default values differently. In .NET 6.0 Visual Basic introduces a new application event ApplyApplicationDefaults which allows you to define application-wide settings (e.g., HighDpiMode or the default font) in the typical Visual Basic way. The designer support for the default font configured via MSBuild properties is also coming in the near future. For more details head over to the dedicated Visual Basic blog post discussing what’s new in Visual Basic.

 

Template updates

As mentioned above we have updated our C# templates in line with related changes in .NET workloads, Windows Forms templates for C# have been updated to support global using directives, file-scoped namespaces, and nullable reference types. Because a typical Windows Forms app requires a STAThread attribute and consist of multiple types split across multiple files (e.g., Form1.cs and Form1.Designer.cs) the top-level statements are notably absent from the Windows Forms templates. However, the updated templates do include the application bootstrap code.

Templates. What’s new in Windows Forms in .NET 6.0

 

More runtime designers

We have completed porting missing designers and designer-related infrastructure that enable building a general-purpose designer (e.g., a report designer). For more details refer to our earlier announcement.

Tiny Designer by Paolo Foti

If you think we missed a designer that your application depends on, please let us know at our GitHub repository.

 

High DPI and scaling fixes

We’ve been working through the high DPI space with the aim to get Windows Forms applications to correctly support PerMonitorV2 mode out of the box. It is a challenging undertaking, and sadly we couldn’t achieve as much as we’d hoped. Still in this release we made some progress, and we now can:

  • Create controls in the same DPI awarenes as the application
  • Correctly scale ContainerControls and MDI child windows in PerMonitorV2 mode in most scenarios. There are still few specific scenarios (e.g., anchoring) and controls (e.g., MonthCalendar) where the experience remains subpar. Scaling, What’s new in Windows Forms in .NET 6.0

 

Other notable changes

  • New overloads for Control.Invoke() and Control.BeginInvoke() methods that take Action and Func<T> and allow writing more modern and concise code.
  • New Control.IsAncestorSiteInDesignMode API is complimentary to Component.DesignMode, and indicates if one of the ancestors of this control is sited, and that site in design mode. A dedicated blog post exploring this API is coming later, so stay tuned.
  • Windows 11 style default tooltip behavior makes the tooltip remain open when mouse hovers over it, and not disappear automatically. The tooltip can be dismissed by CONTROL or ESCAPE keys.

 

Community contributions

We’d like to call out a few community contributions:

 

Reporting bugs and suggesting features

If you have any comments, suggestions or faced some issues, please let us know! Submit Visual Studio and Designer related issues via Visual Studio Feedback (look for a button in the top right corner in Visual Studio), and Windows Forms runtime related issues at our GitHub repository.

Happy coding!

23 comments

Leave a comment

  • RedShark

    “We know, the form in the designer still has that Windows XP look, We’re working on it…” – did you mean Windows 7? cause Windows XP never had that look…
    Also, please add native dark mode to WinForms applications!

    • Igor VelikorossovMicrosoft employee

      Thank you, fixed. It’s been awhile since I used either.

      The team is acutely aware of the desire for the theming of Win32 apps, unfortunately it has dependencies on Windows API, and there are challenges resolving this. We’re actively engaged with our partners in Windows but don’t have an ETA at this time.

      • Reilly Wood

        unfortunately it has dependencies on Windows API, and there are challenges resolving this

        To elaborate, I believe the main challenge is that Windows still does not have a documented way to check whether the system is in light or dark mode. Please correct me if I’m wrong about that.

  • Daniel Smith

    Congrats to everyone on the WinForms team for a great release! The per monitor high DPI work sounds especially challenging, and it’s totally understandable that it’ll take time to cover all the nuances.

    Is there a roadmap ahead for .NET 7? The top feature for me would be to re-enable support for the Data Sources window. Being able to drag and drop classes from that window, and have the controls automatically created and data bound is a huge time saver.

    • Merrie McGawMicrosoft employee

      I totally echo what Igor says here, and I’d also encourage you to keep an eye out for a similar post talking about WinForms Designer in VS2022. We’ve added some features around data that I think you might like 😁. Our first VS related blogpost should be published sometime next month.

    • schbaem

      I don’t know, it would have been nice to have the high DPI fixes in the long term release. It was clear at the start of the .NET6 development cycle that HDPI support in winforms is a desaster. Once you have monitors with different DPIs each, the default framework windows/forms/controls start to look and behave like total junk.

    • Muhammad Miftah

      The lack of .NET 6 specific news for WPF does not mean it is dead; alot of the hard work that went into porting WPF to .NET Core happened back during the .NET Core 3.* release. When Microsoft ported WPF+WinForms to .NET Core, they probably had to leave a lot out for WinForms initially, and they’re now finally getting around to it this release.

      Microsoft did recently open source the unit testing suite for WPF: https://github.com/dotnet/wpf-test

      • Fabien Geraud

        alot of the hard work that went into porting WPF to .NET Core

        Done on .Net core 3. There was no work on this on .Net core 5 or .Net 6. You can look the commit on github. There is no commit on this.

        Microsoft did recently open source the unit testing suite for WPF

        Officially that on what they work on for the last 3 years (Again you can read all the commit done the last three years. It may seems like a big job but there is less thant one real commit a month so it will be quick). No work on anythinks else. Except in visual studio with the new designer and some improvement on intellisense for xaml.

    • Max Mustermueller

      Yes it is. Look at the new roadmap for 2022. There is no new stuff coming, no improvements. And meanwhile ALL community PRs which adds new code are left
      completely unreviewed. On some of them the authors have just closed the PR because after more than half a year there was no activity from the “WPF team”.

    • Huo Yaoyuan

      As I inferred from some talks, there is a shared team for xaml UI. There are plenty of work done for WinUI this year, and many things are still on backlog. I’m afraid there’s no enough people to work on WPF.

  • Guido Geurts

    It would be nice if they would fix bugs first, before adding new stuff.
    Visual Inheritance is still a nightmare in the designer, please finally fix that.

  • sergio

    Form Designer in release VS 2022 works much better, it was way too slow I checked it last time some months ago. Thank you.

    Is there any special reason why Toolbox shows Framework custom controls disabled for Net 6.0 project? My Net 6.0 app uses some my controls from Net Framework 4.5 dll. When added to the form by the code, the controls seem work just fine, both in design- and run-time. The only problem is that Toolbox shows them grayed out. I see no clear reason for that: once I can manage the controls on the form (copy/paste them, change properties, delete) why can’t they be dragged from Toolbox?

  • Rob Ainscough

    Thank you for the update/blog, but still no mention of DirectX 11 or 12 or (13 for 2022) native support in .NET 6. I still don’t understand why Microsoft are avoiding DirectX in .NET? From a competitive standpoint, we have Vulkan for Java via LWJGL – Khronos.

    MDX, XNA, SharpDX, SlimDX are all dead in the water

    DirectX still seems to be exclusive to C++ (not C++.NET) … I don’t see how keeping DirectX restricted to a very a narrow development base as being good for Microsoft?

    Cheers, Rob.