Skip to main content

What’s new in Windows Forms in .NET 6.0

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 XP 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!

The post What’s new in Windows Forms in .NET 6.0 appeared first on .NET Blog.



Comments

Popular posts from this blog

Fake CVR Generator Denmark

What Is Danish CVR The Central Business Register (CVR) is the central register of the state with information on all Danish companies. Since 1999, the Central Business Register has been the authoritative register for current and historical basic data on all registered companies in Denmark. Data comes from the companies' own registrations on Virk Report. There is also information on associations and public authorities in the CVR. As of 2018, CVR also contains information on Greenlandic companies, associations and authorities. In CVR at Virk you can do single lookups, filtered searches, create extracts and subscriptions, and retrieve a wide range of company documents and transcripts. Generate Danish CVR For Test (Fake) Click the button below to generate the valid CVR number for Denmark. You can click multiple times to generate several numbers. These numbers can be used to Test your sofware application that uses CVR, or Testing CVR APIs that Danish Govt provide. Generate

How To Iterate Dictionary Object

Dictionary is a object that can store values in Key-Value pair. its just like a list, the only difference is: List can be iterate using index(0-n) but not the Dictionary . Generally when we try to iterate the dictionary we get below error: " Collection was modified; enumeration operation may not execute. " So How to parse a dictionary and modify its values?? To iterate dictionary we must loop through it's keys or key - value pair. Using keys

How To Append Data to HTML5 localStorage or sessionStorage?

The localStorage property allows you to access a local Storage object. localStorage is similar to sessionStorage. The only difference is that, while data stored in localStorage has no expiration time untill unless user deletes his cache, data stored in sessionStorage gets cleared when the originating window or tab get closed. These are new HTML5 objects and provide these methods to deal with it: The following snippet accesses the current domain's local Storage object and adds a data item to it using Storage.setItem() . localStorage.setItem('myFav', 'Taylor Swift'); or you can use the keyname directly as : localStorage.myFav = 'Taylor Swift'; To grab the value set in localStorage or sessionStorage, we can use localStorage.getItem("myFav"); or localStorage.myFav There's no append function for localStorage or sessionStorage objects. It's not hard to write one though.The simplest solution goes here: But we can kee