C #. Net custom control creation tutorial

Source: Internet
Author: User

. 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

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.