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
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
andListView
column header controls. - Keyboard tooltips for the
TabControl
’sTabPage
and theTreeView
’sTreeNode
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
andNumericUpDown
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:
(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.
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.
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
ContainerControl
s 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.
Other notable changes
- New overloads for
Control.Invoke()
andControl.BeginInvoke()
methods that takeAction
andFunc<T>
and allow writing more modern and concise code. - New
Control.IsAncestorSiteInDesignMode
API is complimentary toComponent.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:
- @paul1956 updated
NotifyIcon.Text
limits text to 127 (dotnet/winforms#4363). - @weltkante enhanced
FolderBrowserDialog
withInitialDirectory
andClientGuid
properties in dotnet/winforms#4645. - @weltkante added link span to
LinkClickedEventArgs
(dotnet/winforms#4708) making it easier to migrateRichTextBox
functionality targeting RichEdit v3.0 or below that relied on hidden text to render hyperlinks. - @AraHaan updated the good old
MessageBox
with two new buttonsTry Again
andContinue
, and made it possible to show four buttons at the same time (dotnet/winforms#4746): - @kant2002 was helping us making Windows Forms runtime more ILLink/NativeAOT-friendlier by adding ComWrappers and removing redundant RCWs. (dotnet/winforms#5174 and dotnet/winforms#4971).
- @kirsan31 provided the ability to anchor minimized MDI children to TopLeft to match Windows MFC behavior in dotnet/winforms#5221.
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!
What about data-binding?
I’ve an old WinForms app which I’m desperate to move forwards. Is Core just a wrapper and I carry on writing in Framework, do I have to port Framework to Core or can I write both side by side?
I’m particularly interested in knowing what the score is with data is having written everything in ADO.NET and stored procedures in MSSQL, not particularly wishing to go down the EF route.
.NET6 Ado.nt When using the Dataset interface with Visual Studio 2019 / .NET 6 projects, you might get the error “Error Using the dropdown. The connection to the server has been lost.” when trying to access project data sources.
Can you fix it..
Please submit issues with VS and Windows Forms designer via the VS Feedback (located at the top right hand side corner of VS).
Hello, I have an application written with .net framework 4.8, for many factors I can't make it compatible with devices that have high DPI. Now I wanted to rewrite everything with visual studio 2022 using .net 6. (I don't digest UWP and WPF XD) Do you have any advice to give in this regard? Using VS 2022 .Net6 I have seen that in the winforms design you have removed the dpi scaling warning, I suppose...
Unfortunately for .NET applications we had to remove the goldbar from VS because restarting with
/noscale
made the new .NET designer literally unusable – processes with different DPI settings don’t play nicely. We are exploring alternatives, in mean time the recommendation is to design at 100% scale factor.(Please note the goldbar is still there for the .NET Framework designer.)
I will follow your advice to keep designing with 100% scale. Igor, do your best with your team. Know that you have millions of developers all over the world who have been waiting too long for a solution to solve this problem of high DPI, which as time passes becomes more and more burdensome, I would say from now on I would also define it as serious. It is a problem to distribute applications, and...
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...
You can take a look at TerraFX. https://github.com/terrafx/terrafx , That library also has Vulkan if you want it.
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...
Is it possible that WinForm supports Linux?
Use AvalonaUI, it’s very good.
There are no official plans to support other OS than Windows.
Windows 11 supports
good news
Special thanks to Paul M Cohen @ paul 1956
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.
it WPF dead ? there no news from .net 5/6 side
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.
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”.
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
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...