C#--Custom controls(1)

1. 3 ways to customize controls

    (1) Composite controls: Combine standard controls

             class  MyControl : UserControl{}

    (2) Extended controls: inherit standard controls

             class  MyButton : Button{}

    (3) Custom control: completely customize a control

              class  MyControl: Control{}

2. Make a simple control

    (1) Create a new Winform project first;

    (2) Add a new class to the project;

    (3) Write some custom properties or events of custom controls in the class, as follows:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MyContriol__Text
{
    class MyText : Control
    {
        public MyText()
        {
            this.BackColor = Color.Blue;
            this.Size = new Size(20,20);
        }

    }
}

    The MyText class inherits the Control class. The above code generates a rectangular box with a size of 20x20 and a blue background color.

    Note: 1), add attributes in the class constructor; 2), the Control used, need to introduce the namespace using System.Windows.Forms; 3), the Color used, need to introduce the namespace using System.Drawing;

3. Add custom controls

    (1) Regenerate the project solution;

    (2) Reopen the form design interface, and the control can be displayed in the toolbox. If it is not displayed in the toolbox, you need to make the following settings:

        Tools —> Options —> Windows Form Designer —> General —> Auto Fill Toolbox, set True.

 

       The above is an introduction to custom controls. It may be a bit rough. It is just a summary of self-learning custom controls on the evening of March 28, 2020. Based on subsequent learning, it will be updated continuously.

      "Custom control self-study video link" , I would like to thank station B, which provides a lot of tutorials, is a good platform.

 

 

Guess you like

Origin blog.csdn.net/zwb_578209160/article/details/105182361