Introduction to creating user controls in Winform

  • 2020-05-10 18:38:16
  • OfStack

Steps:

1. Create a project, which is mainly used to design user controls.

2. Create a user control form to design user controls.

3. Add a button (button1) to the form of user control, add the corresponding moving in and out events to it, and set the background of the button to 1 picture when moving in, and another picture when moving out.


   private void button1_MouseEnter(object sender, EventArgs e)
        {
            this.button1.Image = Image.FromFile(@"images\ The background image 001.jpg");
        }
        private void button1_MouseLeave(object sender, EventArgs e)
        {
            this.button1.Image = Image.FromFile(@"images/ The background image 003.jpg");
        }

4. Override the Text properties of the parent class for the user control (actually setting and getting the text of button1)

    // Overrides the parent class Text attribute 
        public override string Text
        {
            get
            {
                return button1.Text;
            }
            set
            {
                button1.Text = value;
            }
        }

5. Customize a property for the user control (when using the user control, you will see an age property in the property panel, which can be run by yourself. See 1)

    // The custom 1 A property 
        [Category(" The custom "), Description(" Display text content ")]
        public string age
        {
            get { return "aaa"; }
            set { button1.Text = value; }
        }

6, so a simple user control is done

7. Create another form project to reference our user control above

8, add the user control to the form, run can see the corresponding effect.


Related articles: