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.