. Net user-defined controls inherit the usercontrol class and are designed very simple, just like dragging controls on a form.
Follow me step by step:
1. Create a project and add a user control.
2. Enter the control name in the open form, for example, "ucbutton". Click OK. Next, drag and drop three. Net controls in the blank area.
For example:
3. Encoding
Code
/// <Summary>
/// C #. Net Design User-Defined controls
/// C # create a custom control (by www.vjsdn.net)
/// </Summary>
/// </Summary>
[Toolboxbitmap (typeof (customcontrol. ucbutton), "ucbutton.bmp")]
Public partial class ucbutton: usercontrol
{
Private bool _ isfocused = false; // indicates whether the button is in focus state.
Public ucbutton ()
{
Initializecomponent ();
This. dohidefocusedtag ();
This. mycatpiontext = This. Name;
}
Private eventhandler _ onbuttonclick = NULL;
Private string _ mycatpiontext = "ucbutton1 ";
/// <Summary>
/// Button title
/// </Summary>
[Editorbrowsable (editorbrowsablestate. Always)]
[Browsable (true)]
[Defaultvalue ("ucbutton1")]
Public String mycatpiontext
{
Get {return _ mycatpiontext ;}
Set {_ mycatpiontext = value; lblcaption. Text = _ mycatpiontext ;}
}
/// <Summary>
/// Custom Click Event
/// </Summary>
[Editorbrowsable (editorbrowsablestate. Always)]
[Browsable (true)]
Public event eventhandler onbuttonclick
{
Add {_ onbuttonclick + = new eventhandler (value );}
Remove {_ onbuttonclick-= new eventhandler (value );}
}
Private void lblcaption_click (Object sender, eventargs E)
{
// Transfer the click event to trigger the Custom Event
If (_ onbuttonclick! = NULL) _ onbuttonclick (this, e );
}
Private void lblcaption_mousedown (Object sender, mouseeventargs E)
{
If (_ isfocused)
{
Lblcaption. font = new font (lblcaption. Font. fontfamily, lblcaption. Font. Size, fontstyle. Bold );
}
}
Private void lblcaption_mouseup (Object sender, mouseeventargs E)
{
If (_ isfocused)
{
Lblcaption. font = new font (lblcaption. Font. fontfamily, lblcaption. Font. Size, fontstyle. Regular );
}
}
Private void ucbutton_sizechanged (Object sender, eventargs E)
{
Lblunderline. Top = This. Height-1;
Lblunderline. width = This. Width-15;
}
/// <Summary>
/// Restore button status
/// </Summary>
Public void dohidefocusedtag ()
{
This. picturebox1.image = Global: vjsdn. customcontrol. properties. Resources. graytag;
This. lblunderline. Visible = false;
Lblcaption. forecolor = color. Black;
}
/// <Summary>
/// The design button is in focus state.
/// </Summary>
Public void doshowfocusedtag ()
{
This. picturebox1.image = Global: vjsdn. customcontrol. properties. Resources. fosedtag;
This. lblunderline. Visible = true;
Lblcaption. forecolor = color. blue;
}
Private void ucbutton_mouseenter (Object sender, eventargs E)
{
If (this. parent! = NULL)
{
Foreach (control C in this. Parent. Controls)
{
If (C is ucbutton) (C as ucbutton). dohidefocusedtag ();
}
}
This. doshowfocusedtag ();
_ Isfocused = true;
}
[Editorbrowsable (editorbrowsablestate. Always)]
[Browsable (true)]
[Description ("")]
Public label mycaption
{
Get {return lblcaption ;}
}
Private void lblcaption_mouseenter (Object sender, eventargs E)
{
This. ucbutton_mouseenter (sender, e );
}
}
4. Press F5 to compile the project and create a test form. A project with a gear icon is displayed on the control toolbar.
Drag 3 ucbutton on the form.
5. Set button titles and events.
6. Run the program