{"id":37516,"date":"2021-11-16T08:00:31","date_gmt":"2021-11-16T15:00:31","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/dotnet\/?p=37516"},"modified":"2021-11-16T17:24:37","modified_gmt":"2021-11-17T00:24:37","slug":"whats-new-in-windows-forms-in-net-6-0","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet\/whats-new-in-windows-forms-in-net-6-0\/","title":{"rendered":"What&#8217;s new in Windows Forms in .NET 6.0"},"content":{"rendered":"<p>We continue to support and innovate in Windows Forms runtime. Let&#8217;s recap what we&#8217;ve done in .NET 6.0.<\/p>\n<h2>Accessibility improvements and fixes<\/h2>\n<p><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2021\/11\/a11y.png\" alt=\"Accessibility improvements, What\u2019s new in Windows Forms in .NET 6.0\" width=\"711\" height=\"352\" \/><\/p>\n<p>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:<\/p>\n<ul>\n<li>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.\nWe have now added UIA providers support for the following controls:<\/p>\n<ul>\n<li><code>CheckedListBox<\/code><\/li>\n<li><code>LinkLabel<\/code><\/li>\n<li><code>Panel<\/code><\/li>\n<li><code>ScrollBar<\/code><\/li>\n<li><code>TabControl<\/code><\/li>\n<li><code>TrackBar<\/code><\/li>\n<\/ul>\n<\/li>\n<li>Improved Narrator announcements in <code>DataGridView<\/code>, <code>ErrorProvider<\/code> and <code>ListView<\/code> column header controls.<\/li>\n<li>Keyboard tooltips for the <code>TabControl<\/code>\u2019s <code>TabPage<\/code> and the <code>TreeView<\/code>\u2019s <code>TreeNode<\/code> controls.<\/li>\n<li><a href=\"https:\/\/docs.microsoft.com\/windows\/win32\/winauto\/uiauto-implementingscrollitem\">ScrollItem Control Pattern<\/a> support for <code>ComboBoxItemAccessibleObject<\/code>.<\/li>\n<li>Corrected control types for better support of <a href=\"https:\/\/docs.microsoft.com\/windows\/win32\/winauto\/uiauto-implementingtextandtextrange\">Text Control Patterns<\/a>.<\/li>\n<li><a href=\"https:\/\/docs.microsoft.com\/windows\/win32\/winauto\/uiauto-implementingexpandcollapse\">ExpandCollapse Control Pattern<\/a> support for the <code>DateTimePicker<\/code> control.<\/li>\n<li><a href=\"https:\/\/docs.microsoft.com\/windows\/win32\/winauto\/uiauto-implementinginvoke\">Invoke Control Pattern<\/a> support for the UpDownButtons component in <code>DomainUpDown<\/code> and <code>NumericUpDown<\/code> controls.<\/li>\n<li>Improved color contrast in the following controls:\n<ul>\n<li><code>CheckedListBox<\/code><\/li>\n<li><code>DataGridView<\/code><\/li>\n<li><code>Label<\/code><\/li>\n<li><code>PropertyGridView<\/code><\/li>\n<li><code>ToolStripButton<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h2>Application bootstrap<\/h2>\n<p>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 (<a href=\"https:\/\/github.com\/dotnet\/winforms\/pull\/656\">dotnet\/winforms#656<\/a>), 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 <a href=\"https:\/\/docs.microsoft.com\/dotnet\/core\/compatibility\/fx-core#default-control-font-changed-to-segoe-ui-9-pt\">migration strategies<\/a>, applying those across hundreds of forms and controls could be a significant undertaking.<\/p>\n<p>To make it easier to migrate those pixel-perfect apps we introduced a new API (for more details refer to the <a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/whats-new-in-windows-forms-in-net-6-0-preview-5\/#application-wide-default-font\">Application-wide default font<\/a> post):<\/p>\n<pre><code class=\"language-csharp\">void Application.SetDefaultFont(Font font)<\/code><\/pre>\n<p>However, this API wasn\u2019t sufficient to address the designer\u2019s 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 <code>Program.cs<\/code> and its <code>Main()<\/code> method started looking very dated, and we decided to follow the general .NET trend and trim the boilerplate. Please welcome the new <a href=\"https:\/\/github.com\/dotnet\/designs\/blob\/main\/accepted\/2021\/winforms\/streamline-application-bootstrap.md\">Windows Forms application bootstrap<\/a>:<\/p>\n<pre style=\"padding-left: 40px;\"><code class=\"language-csharp\">class Program\r\n{\r\n    [STAThread]\r\n    static void Main()\r\n    {\r\n        ApplicationConfiguration.Initialize();\r\n        Application.Run(new Form1());\r\n    }\r\n}<\/code><\/pre>\n<p><code>ApplicationConfiguration.Initialize()<\/code> is a source generated API that behind the scenes emits the following calls:<\/p>\n<pre style=\"padding-left: 40px;\"><code class=\"language-csharp\">Application.EnableVisualStyles();\r\nApplication.SetCompatibleTextRenderingDefault(false);\r\nApplication.SetDefaultFont(new Font(...));\r\nApplication.SetHighDpiMode(HighDpiMode.SystemAware);<\/code><\/pre>\n<p>The parameters of these calls are configurable via <a href=\"https:\/\/docs.microsoft.com\/dotnet\/core\/project-sdk\/msbuild-props-desktop#windows-forms-settings\">MSBuild properties<\/a> in csproj or props files.\nThe 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:<\/p>\n<p><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2021\/11\/appbootstrap.png\" alt=\"Application bootstrap. What\u2019s new in Windows Forms in .NET 6.0\" width=\"980\" height=\"993\" \/><\/p>\n<p style=\"text-align: center;\">(<em>We know, the form in the designer still has that Windows 7 look, We&#8217;re working on it&#8230;<\/em>)<\/p>\n<p>Please note that Visual Basic handles these application-wide default values differently. In .NET 6.0 Visual Basic introduces a new application event <a href=\"https:\/\/docs.microsoft.com\/dotnet\/api\/microsoft.visualbasic.applicationservices.windowsformsapplicationbase.applyapplicationdefaults\"><code>ApplyApplicationDefaults<\/code><\/a> which allows you to define application-wide settings (e.g., <code>HighDpiMode<\/code> or the default font) in the <a href=\"https:\/\/docs.microsoft.com\/dotnet\/api\/microsoft.visualbasic.applicationservices.applyapplicationdefaultseventargs\">typical Visual Basic way<\/a>. 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 <a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/whats-new-for-visual-basic-in-visual-studio-2022\">what&#8217;s new in Visual Basic<\/a>.<\/p>\n<p>&nbsp;<\/p>\n<h2>Template updates<\/h2>\n<p>As mentioned above we have updated our C# templates in line with <a href=\"https:\/\/docs.microsoft.com\/dotnet\/core\/compatibility\/sdk\/6.0\/csharp-template-code\">related changes in .NET workloads<\/a>, Windows Forms templates for C# have been updated to support <code>global using<\/code> directives, file-scoped namespaces, and nullable reference types. Because a typical Windows Forms app requires a <code>STAThread<\/code> attribute and consist of multiple types split across multiple files (e.g., <code>Form1.cs<\/code> and <code>Form1.Designer.cs<\/code>) the top-level statements are notably absent from the Windows Forms templates. However, the updated templates do include the application bootstrap code.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2021\/11\/templates.png\" alt=\"Templates. What\u2019s new in Windows Forms in .NET 6.0\" width=\"1310\" height=\"774\" \/><\/p>\n<p>&nbsp;<\/p>\n<h2>More runtime designers<\/h2>\n<p>We have completed porting missing designers and designer-related infrastructure that enable building a <em>general-purpose designer<\/em> (e.g., a report designer). For more details refer to our earlier <a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/whats-new-in-windows-forms-in-net-6-0-preview-5\/#more-runtime-designers\">announcement<\/a>.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2021\/11\/tinydesigner.png\" alt=\"Tiny Designer by Paolo Foti\" width=\"600\" height=\"403\" \/><\/p>\n<p>If you think we missed a designer that your application depends on, please let us know at our <a href=\"https:\/\/github.com\/dotnet\/winforms\">GitHub repository<\/a>.<\/p>\n<p>&nbsp;<\/p>\n<h2>High DPI and scaling fixes<\/h2>\n<p>We&#8217;ve been working through the high DPI space with the aim to get Windows Forms applications to correctly support <a href=\"https:\/\/docs.microsoft.com\/windows\/win32\/hidpi\/high-dpi-desktop-application-development-on-windows#per-monitor-and-per-monitor-v2-dpi-awareness\">PerMonitorV2 mode<\/a> out of the box. It is a challenging undertaking, and sadly we couldn&#8217;t achieve as much as we&#8217;d hoped. Still in this release we made some progress, and we now can:<\/p>\n<ul>\n<li>Create controls in the same DPI awarenes as the application<\/li>\n<li>Correctly scale <code>ContainerControl<\/code>s and MDI child windows in PerMonitorV2 mode in most scenarios. There are still few specific scenarios (e.g., anchoring) and controls (e.g., <code>MonthCalendar<\/code>) where the experience remains subpar.\n<img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2021\/11\/scaling.png\" alt=\"Scaling, What\u2019s new in Windows Forms in .NET 6.0\" width=\"1200\" height=\"600\" \/><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h2>Other notable changes<\/h2>\n<ul>\n<li>New overloads for <a href=\"https:\/\/docs.microsoft.com\/dotnet\/api\/system.windows.forms.control.invoke\"><code>Control.Invoke()<\/code><\/a> and <a href=\"https:\/\/docs.microsoft.com\/dotnet\/api\/system.windows.forms.control.begininvoke\"><code>Control.BeginInvoke()<\/code><\/a> methods that take <code>Action<\/code> and <code>Func&lt;T&gt;<\/code> and allow writing more modern and concise code.<\/li>\n<li>New <a href=\"https:\/\/docs.microsoft.com\/dotnet\/api\/system.windows.forms.control.isancestorsiteindesignmode\"><code>Control.IsAncestorSiteInDesignMode<\/code><\/a> API is complimentary to <a href=\"https:\/\/docs.microsoft.com\/dotnet\/api\/system.componentmodel.component.designmode\"><code>Component.DesignMode<\/code><\/a>, 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.<\/li>\n<li>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.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h2>Community contributions<\/h2>\n<p>We\u2019d like to call out a few community contributions:<\/p>\n<ul>\n<li><a href=\"https:\/\/github.com\/paul1956\">@paul1956<\/a> updated <a href=\"https:\/\/docs.microsoft.com\/dotnet\/api\/system.windows.forms.notifyicon.text\"><code>NotifyIcon.Text<\/code><\/a> limits text to 127 (<a href=\"https:\/\/github.com\/dotnet\/winforms\/pull\/4363\">dotnet\/winforms#4363<\/a>).<\/li>\n<li><a href=\"https:\/\/github.com\/weltkante\">@weltkante<\/a> enhanced <a href=\"https:\/\/docs.microsoft.com\/dotnet\/api\/system.windows.forms.folderbrowserdialog\"><code>FolderBrowserDialog<\/code><\/a> with <code>InitialDirectory<\/code> and <code>ClientGuid<\/code> properties in <a href=\"https:\/\/github.com\/dotnet\/winforms\/pull\/4645\">dotnet\/winforms#4645<\/a>.<\/li>\n<li><a href=\"https:\/\/github.com\/weltkante\">@weltkante<\/a> added link span to <a href=\"https:\/\/docs.microsoft.com\/dotnet\/api\/system.windows.forms.linkclickedeventargs\"><code>LinkClickedEventArgs<\/code><\/a> (<a href=\"https:\/\/github.com\/dotnet\/winforms\/pull\/4708\">dotnet\/winforms#4708<\/a>) making it easier to migrate <code>RichTextBox<\/code> functionality targeting RichEdit v3.0 or below that relied on hidden text to render hyperlinks.<\/li>\n<li><a href=\"https:\/\/github.com\/AraHaan\">@AraHaan<\/a> updated the good old <code>MessageBox<\/code> with two new buttons <code>Try Again<\/code> and <code>Continue<\/code>, and made it possible to show four buttons at the same time (<a href=\"https:\/\/github.com\/dotnet\/winforms\/pull\/4746\">dotnet\/winforms#4746<\/a>):\n<img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2021\/11\/messagebox.png\" alt=\"MessageBox\" \/><\/li>\n<li><a href=\"https:\/\/github.com\/kant2002\">@kant2002<\/a> was helping us making Windows Forms runtime more ILLink\/NativeAOT-friendlier by adding <a href=\"https:\/\/docs.microsoft.com\/dotnet\/standard\/native-interop\/com-wrappers\">ComWrappers<\/a> and removing redundant RCWs. (<a href=\"https:\/\/github.com\/dotnet\/winforms\/pull\/5174\">dotnet\/winforms#5174<\/a> and <a href=\"https:\/\/github.com\/dotnet\/winforms\/pull\/4971\">dotnet\/winforms#4971<\/a>).<\/li>\n<li><a href=\"https:\/\/github.com\/kirsan31\">@kirsan31<\/a> provided the ability to anchor minimized MDI children to TopLeft to match Windows MFC behavior in <a href=\"https:\/\/github.com\/dotnet\/winforms\/pull\/5221\">dotnet\/winforms#5221<\/a>.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h2>Reporting bugs and suggesting features<\/h2>\n<p>If you have any comments, suggestions or faced some issues, please let us know! Submit Visual Studio and Designer\nrelated issues via <strong>Visual Studio Feedback<\/strong> (look for a button in the top right corner in Visual Studio), and Windows\nForms runtime related issues at our <a href=\"https:\/\/github.com\/dotnet\/winforms\">GitHub repository<\/a>.<\/p>\n<p>Happy coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Read about the new features that are in Windows Forms in .NET 6.0.<\/p>\n","protected":false},"author":13586,"featured_media":37517,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[685,196,7163],"tags":[],"class_list":["post-37516","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dotnet","category-dotnet-core","category-winforms"],"acf":[],"blog_post_summary":"<p>Read about the new features that are in Windows Forms in .NET 6.0.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/37516","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/users\/13586"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/comments?post=37516"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/37516\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media\/37517"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media?parent=37516"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/categories?post=37516"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/tags?post=37516"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}