How to create WinForm program with. Net core 3.0

Time:2020-9-14

A while ago, I had been looking forward to the official version of. Net core 3.0. I thought that this version would bring a new life to WinForm program. However, when I came out on September 23, I immediately downloaded and updated vs to create a new. Net core WinForm project. I found that there was no WinForm form form designer. At present, Microsoft lets us download WinForm designer separately through plug-ins. This designer is still a preview version, and many functions are not implemented. It can only be regarded as a simple prototype. This blog case introduces how to create an ordinary WinForm program based on. Net core3.0, so that you can understand the general appearance of the program created based on. Net core3.0.

1. Preparation of development environment

To develop WinForm based on. Net core 3.0, you need to update your visual studio to 16.3. This version integrates. Net core 3.0, so it is also the basis for developing. Net core WinForm program.

The second is to download the WinForms designer plug-in, which supports the WinForm form form designer, so that we can design and develop the interface by dragging the control.

The. Net core Windows Forms visual designer must be a part of the future visual studio 2019 update in the future, but at present, if you want to visualize the designer, you need a pre released visual studio extension.

After these two steps are completed, other development will be the same as creating a vs project.

After creating the project, we can open the corresponding WinForm form form and see some WinForm interface controls in the toolbox. The good thing is that the controls are almost the same as before. The bad thing is that there are a lot of conventional WinForm controls. This is why the WinForm designer is in the development preview version.

2. Create a WinForm program

In order to create a simple test of the WinForm program, we can add some interface controls to it. However, during the use process, we found that many elements required by the interface did not provide support for the interface control, including the toolbar and properties. For example, the property support of Imagelist object and image object, we can only use it by code.

I created a simple WinForm interface and dragged some regular controls. However, some controls that need to use pictures, such as listview and PictureBox, need to be set by code (pictures cannot be specified by adding attributes)

Finally, the interface display effect is shown as follows.

The source code of the form is shown below.

public partial class Form1 : Form
 {
  public Form1()
  {
   InitializeComponent();
  }

  private void button1_Click(object sender, EventArgs e)
  {
   MessageBox.Show "Hello, this is a WinForm program of. Net core", "prompt message", 
    MessageBoxButtons.OK, MessageBoxIcon.Information | MessageBoxIcon.Asterisk);
  }

   
  private ImageList imageList = new ImageList();
  private void Form1_Load(object sender, EventArgs e)
  {
   var image = Image.FromFile(Path.Combine(Application.StartupPath, "SplashScreen.png"));
   if(image != null)
   {
    this.pictureBox1.Image = image;
   }

   imageList.Images.Clear();
   var iconPath = Path.Combine(Application.StartupPath, "icons");
   var fileNames = Directory.GetFiles(iconPath, "*.ico");
   foreach(string file in fileNames)
   {
    imageList.Images.Add(file, Image.FromFile(file));
   }
   this.treeView1.ImageList = imageList;
   foreach(TreeNode node in this.treeView1.Nodes)
   {
    SetNodeImage(node);
   }

   this.button1.Image = imageList.Images[2];
  }

  private void SetNodeImage(TreeNode node)
  {
   foreach (TreeNode subNode in node.Nodes)
   {
    subNode.ImageIndex = subNode.Level;
    subNode.SelectedImageIndex = subNode.Level;
    SetNodeImage(subNode);
   }
  }

From this, we can see that there is no inconsistent naming of form elements or related objects in. Net core, and it is very convenient and consistent to use. However, it corresponds to the functions of many interfaces. At present, only through the way of background code can we achieve a relatively complete effect, and. Net The development of WinForm under the framework framework is very good. The gap is not a little bit. It seems that. Net core WinForm development still has a long way to go and needs more support at the tool level.

In the interface scheme, we can see that the namespace is much less than before. It is mainly based on the WinForm package provided by. Net core.

Let’s look at the files in the program directory as follows.

At present, we have not considered the third-party. Net core level class library, so we don’t use the third-party DLL here. If we integrate later, the third-party related references will also be a big problem. If most of the commonly used class libraries have the support of class libraries based on. Net standard, it would be good. Otherwise, we may face a dilemma The WinForm development of core is still worth looking forward to. After all, the introduction of an overall. Net core development route is a very good development scenario for enterprises or individuals.

This article on the use of. Net core 3.0 official version of the method to create WinForm procedures to introduce this, more related. Net core 3.0 to create WinForm content, please search the previous articles of developeppaer or continue to browse the related articles below, I hope you can support developeppaer more in the future!