5

We have just moved to .NET and we are trying to recreate one of our old projects with C#/.NET, we have heard that .NET Core is the way to go for new projects and we are very much interested in using WinForms for this, so i have been experimenting with Visual Studio 2019 Preview (16.5.0 Preview 2.0), but i've noticed that not all the controls show up in the designer (i know the designer was just added to the preview), so i tried to add the controls manually on Form1.designer.cs, but im not really sure if this is the right way to do this or should we just wait for more mature releases or just use .NET Framework ? PS: We can go with WPF too if its more supported with .NET Core.

enter image description here

Thank you

Lukasz Szczygielek
  • 2,768
  • 2
  • 20
  • 34
RedZ
  • 857
  • 1
  • 13
  • 25
  • 2
    No - some of the .NET 1.x controls like `Menu` were removed. The changes [are described here](https://learn.microsoft.com/en-us/dotnet/core/compatibility/winforms#removed-controls). Those controls were replaced by others back in 2006 and shouldn't be in use, something explained in the docs. – Panagiotis Kanavos Jan 28 '20 at 16:22
  • by "all the controls show up" you mean "it's not showing `WebBrowser` control" or more? – Selvin Jan 28 '20 at 16:22
  • 5
    `WebBrowser` on the other hand is a different beast - it's IE and *no* site supports IE any more, except by accident. You shouldn't use it even if it's available. We aren't talking about IE 10 either. – Panagiotis Kanavos Jan 28 '20 at 16:23
  • @Selvin its only showing just a few basic controls but i think they just arent supported in the designer , because after adding WebBrowser manually, it showed up in the designer with a big red cross on it – RedZ Jan 28 '20 at 16:23
  • 4
    @RedZ the problem is WebBrowser itself. It's quite likely that whatever site you want to display in it won't work. People are using [CefSharp](https://github.com/cefsharp/CefSharp) instead, which uses the Chromium engine. That project is actively maintained. Don't expect MS to further develop WebBrowser on the other hand, especially now that they replaced even Edge with Chromium – Panagiotis Kanavos Jan 28 '20 at 16:25
  • @PanagiotisKanavos yes you have a point, and i've checked CefSharp, my use of WebBrowser was just to test if the controls work as expected, i guess the right question here is, since the designer isnt ready yet, what is the recommended method of designing forms with .NET Core 3.0 ? – RedZ Jan 28 '20 at 16:57
  • The designer *is* available. In the latest VS 2019 preview you can enable it in `Options > Experimental Features`. It's still experimental though. Furhtermore, the *designer* doesn't tell you whether the controls themselves work or not. The list of breaking changes does – Panagiotis Kanavos Jan 28 '20 at 17:01
  • @Panagiotis Kanavos There are, world-wide, a whole lot of machines that still use Windows7 (and previous versions, too). The WebBrowser control, in IE11 compatibility mode, works still pretty well will all major sites. Of course, you have to plan for the future (as always, as for anything else). – Jimi Jan 28 '20 at 17:03
  • @Jimi it's not a matter if it works or not - the **sites** don't support it. And Windows 7 machines use Edge if they want to avoid compatibility issues. IE exists only for legacy intranet applications – Panagiotis Kanavos Jan 28 '20 at 17:06
  • @Jimi Javascript's [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) for example isn't available in any IE version – Panagiotis Kanavos Jan 28 '20 at 17:07
  • @Jimi I also know from bitter personal experience that redirection bugs in the *old* IE engine used by WebBrowser will prevent you from using login pages to retrieve tokens, with a `Too many redirects` error – Panagiotis Kanavos Jan 28 '20 at 17:09
  • @Panagiotis Kanavos Using Edge in Windows 7 would be a challenge. I'm not saying that the WB control is *gold*, of course, But in a WinForms project (since this is the current context), probably just to show the content of some specific WebSite, it's not that bad, it works in most situations. If you know how to configure it properly, that is. --- You're referring to WebBrowser control in its default mode (IE7). You don't have that error in IE11 mode. Everyboby's waiting for a new WebBrowser control to show up in the ToolBox, though. This one is actually getting old :) – Jimi Jan 28 '20 at 17:15
  • You should also take into account that, porting to .Net core doesn't mean it would run on other platforms. Maybe go with WPF. – Cetin Basoz Jan 28 '20 at 17:19
  • @CetinBasoz i dont really have any interest in cross platforming for now, we just want a stable framework with good UI, and ive just checked WPF (.NET Core) and it seems like the designer works + more controls are included and it uses XAML which is pretty good, so we might go with WPF instead of WinForms – RedZ Jan 28 '20 at 17:23
  • IMO it's a bit soon to migrate to Windows Forms .NET Core. I'd say, start migrating your business logic, but for the UI wait for more stable version. Take a loot at this post about latest release: [Updates to .NET Core Windows Forms designer in Visual Studio 16.5 Preview 1](https://devblogs.microsoft.com/dotnet/updates-to-net-core-windows-forms-designer-in-visual-studio-16-5-preview-1/). – Reza Aghaei Jan 28 '20 at 17:29
  • And my two cents on `WebBrowser` control, You can easily setup `WebBrowser` control to show content in [IE11 Edge mode](https://stackoverflow.com/a/38514446/3110834) which is good enough for most of the sites . As another option you can also use the new [WebViewCompatible control for Windows Forms](https://learn.microsoft.com/en-us/windows/communitytoolkit/controls/wpf-winforms/webviewcompatible?WT.mc_id=DT-MVP-5003235). You can see simple steps to use here: [Replace WebBrowser control by new WebView Compatible control for Windows Forms](https://stackoverflow.com/a/59271260/3110834). – Reza Aghaei Jan 28 '20 at 17:31

2 Answers2

8

Does Winforms (.NET Core 3) support all .NET Framework controls?

No, it doesn't. In fact in .NET Core 3.1 some of the outdated Windows Forms controls like DataGrid, ToolBar, ContextMenu, Menu, MainMenu, MenuItem, and their child components were removed.

You need to use DataGridView, ToolStrip, ContextMenuSrtip, MenuStrip and their child components instead.

To see a list of removed controls and required actions for upgrade take a look at:

I've noticed that not all the controls show up in the designer

Right, the design-time support is still under development and some of the controls and feature are still not available through designer. However if you don't want to use designer, you just need run-time support, use the controls which are supported by .NET CORE 3.1 but don't have design-time support.

Should we just wait for more mature releases or just use .NET Framework?

At time of writing this answer, if you need to use designer on a regular basis, it's still not recommended to port your application to .NET Core. While there is builtin designer support in VS 2019 Version 16.5 Preview 1, but the designer is still under development and some important features like data-binding, some of container controls, localization, MenuStrip and ToolStrip, Visual inheritance and so on ate not available.

To see the list of under development features take a look at:

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • 2
    Thats the answer i was looking for, after checking many answers and doing a bit of research (and reading the articles from microsoft) we decided to go with .NET Core and WPF since we are more comfortable with using XAML (no designer needed) since we also Xamarin the same way. – RedZ Jan 28 '20 at 18:19
  • Why do you recommend using DataGridView with WinForms in .NET Core 3? It is not available! – Jens Mander Jun 21 '20 at 22:53
  • @JensMander Although `DataGridView` still doesn't have design-time support, but you can easily use it in code. Take a look at [this post](https://gunnarpeipman.com/datagridview-dotnet-core-31/) as an example to learn how to use it in code. – Reza Aghaei Jun 22 '20 at 03:42
1

This simple trick can easily activate all the missing controls to start your Winforms .Net Core 3.1

Before you do these steps, just know that is only confirmed for testing, and not for production yet. one It is confirmed for final production application, I will update the post.

this solution is confirmed for me with .Net Framework 4.8 and .net Core 3.1.

with some steps as following: 1- Create your winofrms .Net Core project. 2- Press on project_name.csproj and change this:

    <TargetFrameworks>netcoreapp3.1</TargetFrameworks>

to this new one:

    <TargetFrameworks>net48;netcoreapp3.1</TargetFrameworks>

3- Then open Program.cs and comment this

Application.SetHighDpiMode(HighDpiMode.SystemAware);

To be like this:

//Application.SetHighDpiMode(HighDpiMode.SystemAware);

this solution will add all the controls. just close the solution and reopen.

4- After you finish your application, just change everything to default again like this:

Press on project_name.csproj and change this:

    <TargetFrameworks>netcoreapp3.1</TargetFrameworks>

For the program.cs uncomment, even after I rolled back to default, the application give an error for this line, so I keep it commented

Thanks goes for Kirsan

https://devblogs.microsoft.com/dotnet/updates-to-net-core-windows-forms-designer-in-visual-studio-16-5-preview-1/#comment-4562

Emad Mohamed
  • 97
  • 2
  • 10
  • 1
    I havent had the chance to try this trick yet, but i will very soon, the problem is that the designer for WinForms .NET Core is super buggy, it freezes now and then and sometimes it cant remove/move controls – RedZ Feb 03 '20 at 10:57
  • This is not working at all. Instead a lot of errors pop up and the designer wouldn't start. – Jens Mander Jun 21 '20 at 23:01
  • Well, it has been 8 months since. And I can confirm it is still extremely buggy. Anyone came up with an alternative visual designer for WinForms .NET Core? – Menno van Leeuwen Oct 28 '20 at 23:37