Windows forms app net core

July 18, 2023
1 min read
views 4587
Arthur C. Codex
chats: 2
Engineering
.Net

In this tutorial, we’ll go through the process of creating and deploying a Windows Forms Application using .NET Core.

How to Create and Deploy a Windows Forms Application with .NET Core image

Setting Up the Environment

First, you need to ensure that the .NET Core SDK is installed on your machine. You can download it from the official Microsoft .NET site.

Creating a New Project

Open a new command prompt and navigate to the directory where you’d like to create your new project. Once there, run the following command:


dotnet new winforms -o MyNewApp

This will create a new Windows Forms project in a directory named MyNewApp.

Writing the Application

Next, open Program.cs in your favorite code editor. You’ll see something similar to the code below:


using System;
using System.Windows.Forms;

namespace MyNewApp
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

Let’s add a button to our form. Open Form1.cs and replace the existing code with the following:


using System;
using System.Windows.Forms;

namespace MyNewApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Button button = new Button();
            button.Text = "Click Me";
            button.Click += new EventHandler(Button_Click);
            Controls.Add(button);
        }

        private void Button_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Hello, World!");
        }
    }
}

Deploying the Application

Back in the command prompt, navigate to the project directory and run the following command:


dotnet publish -c Release -r win10-x64 --self-contained

This will create a standalone application that can be run on any Windows 10 x64 machine.

If you need help with more complex applications, you might want to hire .NET remote developers.

Conclusion

And that’s it! You’ve just created and deployed a Windows Forms application using .NET Core. There’s a lot more you can do with .NET Core and Windows Forms, so don’t stop here!

NET Core 3.0 is an open-source version of WinForms and WPF which is bringing Windows desktop development to .NET Core.

The current version of .NET Core 3.0 is available with below (including upcoming) features,

  • Winforms and WPF support
  • Client-Side Web Development with Blazor
  • UI Web Components on the Server Using SignalR
  • EFCore 3.0
  • + Many more features

Today in this article, we will cover below aspects,

  • Getting started
    • Prerequisites:
  • Using WinForms Designer package
  • Summary

Getting started

Prerequisites:

  • Visual Studio 2019 (16.4 and above is preferred)
  • .NET Core 3.0
  • .NET Core Windows Forms Designer Preview 1

Let’s follow step by step process to create a .NET Core Desktop application. Today we will try to create a simple Hello world Forms application using Core 3.0 framework.

  1. If you have not installed Visual Studio VS 2019, Please install it from this location. Currently, it is available as a preview version.
Creating First Form-Desktop Application using .NET Core

  • Visual Studio VS2019 UI look and feel has improved a lot.
  • Please install .NET Core 3.0.
  • Create a new project using the template of your choice.
Creating First Form-Desktop Application using .NET Core

  • Multiple project templates are readily available for .NET core 3.0. Here we will use a simple desktop app template called “Windows Forms App(.NET Core 3.0)”
Creating First Form-Desktop Application using .NET Core

  • Open the project solution and add a new button UI element manually “Click” and the simple message box to it. Then execute your application.
Creating First Form-Desktop Application using .NET Core

Using WinForms Designer package

If you need to use the WinForms Designer package with prebuilt Forms components could be very useful. This package isn’t yet bundled with Visual Studio but available as Visual Studio extension (“VSIX”), please install the same as from below,

https://github.com/dotnet/winforms/tree/master/Documentation/designer-releases

The WinForms Core Designer installation package (“VSIX”) package can be downloaded from here: https://aka.ms/winforms-designer.

Creating First Form-Desktop Application using .NET Core

Post-installation, please restart your Visual studio and you shall be all set.

Creating First Form-Desktop Application using .NET Core

The current preview version has below set of control available which will help you get started at least easily,

  • Pointer
  • Button
  • Checkbox
  • CheckedListBox
  • ComboBox
  • DateTimePicker
  • Label
  • LinkLabel
  • ListBox
  • ListView
  • MaskedTextBox
  • MonthCalendar
  • NumericUpDown
  • PictureBox
  • ProgressBar
  • RadioButton
  • RichTextBox
  • TextBox
  • TreeView

Note: With the release of .NET Core 3.1, Latest Visual Studio 2019, get the built-in basic support for WinForms Core Designer toolkit out of the box. You need not have to install it separately.

Other references:

  • Bind Data to Windows Form GridView control in .NET Core

That’s all, Happy Coding!!

Summary

Please note that it is pretty much the same feel of development using windows forms in .NET Core. But surely the difference now is very impressive .NET Core framework support for your desktop applications, providing all goodies of Core framework plus performance improvements.


Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.


Desktop support for .NET Core 3.0 has been in preview for some time, it is slated to be released later this year. As per indicators by Microsoft, .NET Core will receive more updates compared to full .NET Framework. Keeping this in mind, it makes sense to start thinking about migrating existing applications and creating new ones on .NET Core if the application still has some years to go.

Desktop support in .NET Core has not been completely migrated yet; however, it is possible to create and migrate WinForms applications using the preview bits.

In this article, we’ll create and migrate a WinForms application in .NET Core 3.0 using ComponentOne controls.

Creating a New .NET Core 3.0 Application

To create a new .NET Core 3.0 application, it is recommended to download Visual Studio 2019 and the nightly build of the .NET Core 3.0 SDK.

Make sure that you’ve installed the .NET Framework 4.7.2 and .NET Core 2.1 development tools in the Visual Studio Installer. This software is necessary whether you plan on creating a new project or migrating an existing one to .NET Core 3.0.

One way to work with .NET Core 3.0 (for now) is to use dotnet.exe command line utility. It can be used to create a new project, add/restore dependencies, build, etc. But it is the simplest way is to use Visual Studio 2019 and it’s new Windows Forms App (.NET Core) project template.

Using Visual Studio x64 and x86 Versions

Visual Studio uses the appropriate version depending on your .NET Core 3 project’s target platform (x86 or x64). It uses last installed version for both platforms. So, if you intend to build your project for both platforms you must download and install both platform SDK versions with the same version.

These are the typical paths for .NET Core 3 command line utility:

  • c:\Program Files\dotnet\dotnet.exe — for x64 version of .Net Core 3
  • c:\Program Files (x86)\dotnet\dotnet.exe- for x86 version of .Net Core 3

Create a New Project

First, you’ll need to open the Visual Studio 2019 and create a new Windows Forms App for .NET Core with name: TestAppWinFormsCore:

WinForms and .NET Core 3.0

You should see something like this after the new project has been created:

WinForms and .NET Core 3.0

.NET Core 3.0 uses a new project type and has no design-time support currently, so the process of adding controls will be a little different than what you may be accustomed to and have used in the past. We need to add C1 libraries manually to the project to make use of them. Also, we should add the controls manually to Form1.cs because of missing design-time support.

Because of this, let’s use a trick. We’ll add the classic .NET Framework WinForms project in order to use its Form designer:

  • Right-click on Solution node and Add – New Project… — Windows Forms Desktop project (with .Net Framework, say version 4.0). The name of new project is by default WindowsFormsApp1:

3

  • Remove the existing Form1 from WindowsFormsApp1 project
  • Add Form1.cs from TestAppWinFormsCore project as link:

WinForms and .NET Core 3.0

  • After that the following view of Solution Explorer appears:

WinForms and .NET Core 3.0

Now we can edit Form1 form as usual. We can add some C1 control, for example C1DockingTab:

6

Now you can launch WindowsFormsApp1 application and observe C1DockingTab on Form1.

But this will be a .Net Framework 4 application.
Adding C1DockingTab caused adding C1.Win.C1Command.4 reference to WindowsFormsApp1 project. Therefore, the same reference should be added to TestAppWinFormsCore project also.

For this, let’s take a look in the properties of the C1.Win.C1Command.4 reference (right-click – Properties…) and find the path to the assembly.

It looks like C:\Program Files (x86)\ComponentOne\WinForms Edition\bin\v4.0\C1.Win.C1Command.4.dll.

Add this reference to the TestAppWinFormsCore project (right-click on Dependencies – Add reference — Browse… — choose C:\Program Files (x86)\ComponentOne\WinForms Edition\bin\v4.0\C1.Win.C1Command.4.dll):

7

You should be able to run the code at this point, though you’ll notice a ComponentOne nag screen since theTestAppWinFormsCore project doesn’t contain any licensing information.

We can correct this by adding a licenses.licx file to the project. To do so, right-click on TestAppWinFormsCore project and select Add -> Existing Items. And browse the licenses.licx file from WindowsFormsApp1/Properties folder.

Build and rerun the app and you should no longer see a nag screen.

Now this is a genuine .NET Core 3.0 WinForms app. You can see this in the Modules window. All system references are from C:\Program Files\dotnet… folder.

Migrating an Existing Project to .NET Core 3.0

It is possible to migrate an existing project to .NET Core 3.0, though the process may require a few more steps than you would expect. Also, before trying to migrate a project, it’s worth running Microsoft’s portability analyzer tool on the project you want to convert.

The tool can give you an idea (ahead of time) how compatible your project will be with .NET Core 3.0, and it points out potential problems you may run into (included unsupported APIs).

Let’s look at the WeatherChart sample migration. This sample demonstrates the C1FlexChart control possibilities.
First, we’ll create a new DotNetCore3FlexChart project (Windows Forms App (.NET Core) in Visual Studio 2019 by the same way as described above.

After that, add the WeatherChart project to the DotNetCore3FlexChart solution. WeatherChart project is placed in Documents\ComponentOne Samples\WinForms\C1FlexChart\CS\WeatherChart\WeatherChart\WeatherChart.csproj by default.

8

Here, we’ll use the WeatherChart project in our .NET Core 3.0 project. Therefore, the WeatherChart project reference should be added to DotNetCore3FlexChart dependencies (right-click on Dependencies – Add Reference… — Projects — WeatherChart)

9

After that, open Project.cs from DotNetCore3FlexChart project and change the following line:

Application.Run(new Form1());  
by  
Application.Run(new WeatherChart.Form1());  

It means that the .NET Core 3.0 app (DotNetCore3FlexChart) will run WeatherChart.Form1 from WeatherChart assembly instead of own Form1.

10

That’s all!

Now if you run the WeatherChart project it will be launched as .NET Framework 4.0 app. But if you run the DotNetCore3FlexChart project then the WeatherChart assembly will be launched in .NET Core 3.0 environment.

.NET Core 3.0 Preview Caveats

Using the .NET Core 3.0 Preview for any kind of day-to-day work isn’t recommended at this point, as it’s still work in progress. As the steps above illustrate, much of the project creation and migration processes require a lot of manual configuration from the user, and it’s possible to find some bugs and unfinished implementations presently.

The lack of designer support also makes working with the preview somewhat difficult, though Microsoft has committed to adding this feature in the coming months. Overall, .NET Core 3.0 will be an important change for developers, though this preview is merely the first step.

Download the sample

If you have any questions about this process, please leave a comment below.

Download Now!<%/if%>

Provide feedback

Saved searches

Use saved searches to filter your results more quickly

Sign up

Using the Classic WinForms Designer in WinForms Core

At this point, a dedicated WinForms Designer for WinForms Core is not yet available. As a workaround, you can use Visual Studio’s option to work with linked files and use its WinForms Designer for the Classic Framework.

Here is how it is done.

TIP

During the process, you need to re-nest the Form files in the Classic Framework WinForms project whenever you add a new form or a new UserControl. Instead of using a text editor for patching the project file, you can use Mad Kristensen’s File Nesting Extension, which is recommended to be installed beforehand. Please close every open instance of Visual Studio before installing this extension.

Creating a WinForms Core App

Visual Studio does not allow it currently to create a new WinForms Core App from within Visual Studio directly. If you want to design a WinForms Core app, you need to create the App first from the command line:

Open your favorite console, and create a new folder with the application’s name. The folder name will later become the project’s name as well. Change to that folder.

md MyNewWinFormsProject cd MyNewWinFormsProject

Now, create a new WinForms application with the dotnet new command, using the templates for WinForms.

dotnet new winforms

TIP

You can have the folder name different from the project’s name. Use the option -n (or -name) for that when using dotnet new.

NOTE
Templates for Visual Basic are currently in development (so dotnet new winforms -lang VB will eventually work!), but they are not available yet. Visual Basic, however, is already supported; as a workaround for the time being, you can create an empty Core Console VB app, and rename and patch the project files accordingly (just copy those definitions over from a C# app). Also, note that the Application Framework in Visual Basic is not supported in this version.

After creating it, you can test the application directly by starting it with

dotnet run

which builds and starts the application.

TIP — If you only want to rebuild the application, use:

dotnet build

Preparing the WinForms Core App for the Classic Designer

  1. Start Visual Studio and open this project.

  2. Save the project in Visual Studio, and with that, also save the Solution file.

  3. Open the context menu of the solution (not the project!) in the Solution Explorer and chose Add New Project.

  4. In the Installed Templates section, pick Visual C# (this works for Visual Basic equally; see the comments for Visual Basic above), Windows Desktop, and then click Windows Forms App (.NET Framework) from the list of installed templates.

    Designing WinForms For .NET Core 3.0

  5. Name the new classic Framework project as the Core project, but add .Designer to it (so, for example, if your Core project is named MyWinFormApp, name the classic Framework App MyWinFormsApp.Designer).

  6. In the properties of the Classic Framework App (context menu on the project in the Solution Explorer), rename the default namespace to the Core App’s default namespace.

    Designing WinForms For .NET Core 3.0

  7. Erase the existing Form files in both projects.

The following steps you always repeat, when you need to insert a new Form or a new UserControl,

  1. In the Classing Framework project, open the project’s context menu, and click Add New Item.

    Designing WinForms For .NET Core 3.0

  2. In the section list, click on Windows Forms, and chose Windows Form from the installed templates.

    Designing WinForms For .NET Core 3.0

  3. Enter the name for the new Form/User Control.

  4. Click Add.

    IMPORTANT — Now, cause some change in the Designer on the form. For example, resize the form for a couple of pixels, or change the Text property of the form, so the resource file for it can be generated and saved. After that, save the form.

  5. In the Solution Explorer, click on the form, and press (Ctrl)(x) to cut it the file.

  6. Select the Core WinForms project in the Solution Explorer, and press (Ctrl)(v) to insert the files. Check that the main form file, the .designer file and the resource file for the form are all present.

  7. Now, to have the exact same file back in the WinForms Classic Framework Designer, we need to use Visual Studio’s file link option. Remember: We can only use the Classic Designer, but we want to have only one set of files. So, the form files, of course, belong to the Core App. But we want to edit them in the context of the Classic Framework App (thus using the Classic Designer). So, we link the existing Core Form files to the classic app, and to this end, you open the context menu on the classic project in the solution explorer, and pick Add and Existing Item.

  8. In the File Open Dialog, navigate to the Core app, and find the Form.cs, Form.Designer.cs and Form.resx files. (Replace Form by the name of your form.) Select all of them, but DO NOT click Add yet!

    Designing WinForms For .NET Core 3.0

  9. Open the pulldown menu of the Add dropdown button and click Add as Link.

  10. Compile the solution to see if the file references got set up correctly.

  11. As the last but important step, we need to re-nest the linked form files. If you installed the File Nesting Visual Studio Extension (see above), then that is done easily: Select both the Form.Designer.cs and Form.resx file, and from the context menu click File Nesting and Nest Items. In the dialog, pick the main form file (Form.cs), and click OK.

Now, whenever you need to use the Designer on one of the Core Form or UserControl files, simply open the linked files in the Classic Framework project with the Classic Windows Forms Designer.

Понравилась статья? Поделить с друзьями:
0 0 голоса
Рейтинг статьи
Подписаться
Уведомить о
guest

0 комментариев
Старые
Новые Популярные
Межтекстовые Отзывы
Посмотреть все комментарии
  • Отключить пароль на windows 10 домашняя при входе в систему
  • Usb power consumption windows
  • Windows 10 enterprise ltsb или windows 10 enterprise ltsb
  • Интересные фишки windows 11
  • Что нужно сделать сразу после установки windows 10