Introduction to creating user controls in Winform

Source: Internet
Author: User

Steps:

1. Create a project to design user controls.

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

3. Add a button (button1) to the user control form to add the corresponding migration and removal events to it, so as to set an image for the background of the button during migration, set another image as the background when removing the image.

Copy codeThe Code is as follows: private void button#mouseenter (object sender, EventArgs e)
{
This. button1.Image = Image. FromFile (@ "images \ background Image 001.jpg ");
}

Private void button1_MouseLeave (object sender, EventArgs e)
{
This. button1.Image = Image. FromFile (@ "images/background Image 003.jpg ");
}

4. Rewrite the Text attribute of the parent class for the user control (in fact, it is to set and obtain the Text of button1)Copy codeThe Code is as follows: // rewrite the Text attribute of the parent class
Public override string Text
{
Get
{
Return button1.Text;
}
Set
{
Button1.Text = value;
}
}

5. Define an attribute for the user control (an age attribute is displayed on the property panel when the user control is used, and you can see it by running it yourself)Copy codeThe Code is as follows: // customize an attribute
[Category ("Custom"), Description ("show text content")]
Public string age
{
Get {return "aaa ";}
Set {button1.Text = value ;}
}

6. This simple user control is ready.

7. Create another form project and reference the above User Control

8. Add the user control to the form and run it to view the effect.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.