Custom Controls are divided into three types:
1. Composite controls: A combination of basic controls. Inherited from usercontrol
2. extended controls: inherits basic controls and extends some attributes and events. For example, inherit button
3. Custom Control: directly inherit from control
In the first case, it is easy to get started and is also commonly used. There are also a lot of skills to summarize.
For example, you need to create a single class library project to avoid conflicts.
How to handle events
Code Postponed to the user.
Let's get started with the extended controls today.
------------------------------------------------------------------
Step 1: Create a Windows control library project.
Step 2: create a user control and modify the code (note that the code in the. Designer. CS file is commented out)
Extended button
Using System;
Using System. Collections. Generic;
Using System. componentmodel;
Using System. drawing;
Using System. Data;
Using System. LINQ;
Using System. text;
Using System. Windows. forms;
Namespace Winformcontrollibrary
{
Public Partial Class Ucbutton: button
{
Public Ucbutton ()
{
Initializecomponent ();
}
// Creates the private variable that will store the value of your
// Property.
Private Int Varvalue;
// Declares the property.
Public Int Buttonvalue
{
// Sets the method for retrieving the value of your property.
Get
{
Return Varvalue;
}
// Sets the method for setting the value of your property.
Set
{
Varvalue = value;
}
}
}
}
Modify. desinger. CS Namespace Winformcontrollibrary
{
Partial Class Ucbutton
{
/// <Summary>
/// Required designer variable.
/// </Summary>
Private System. componentmodel. icontainer components = Null ;
/// <Summary>
/// Clean up any resources being used.
/// </Summary>
/// <Param name = "disposing"> True if managed resources shocould be disposed; otherwise, false. </Param>
Protected Override Void Dispose ( Bool Disposing)
{
If (Disposing & (components! = Null ))
{
Components. Dispose ();
}
Base . Dispose (disposing );
}
# RegionComponent designer generated code
/// <Summary>
/// Required Method for designer support-do not modify
/// The contents of this method with the code editor.
/// </Summary>
Private Void Initializecomponent ()
{
Components = New System. componentmodel. Container ();
// Comment out this sentence
// This. autoscalemode = system. Windows. Forms. autoscalemode. Font;
}
# Endregion
}
}
Extended labelUsingSystem;
UsingSystem. Collections. Generic;
UsingSystem. componentmodel;
UsingSystem. drawing;
UsingSystem. Data;
UsingSystem. LINQ;
UsingSystem. text;
UsingSystem. Windows. forms;
Namespace Winformcontrollibrary
{
Public Partial Class Uclabel: Label
{
Public Uclabel ()
{
Initializecomponent ();
}
Protected Override Void Onmouseenter (eventargs E)
{
Base . Onmouseenter (E );
This . Font = New Font ( " " , 10f, fontstyle. Underline );
}
Protected Override Void Onmouseleave (system. eventargs E)
{
Base . Onmouseleave (E );
This . Font = New Font ( " " , 10f, fontstyle. Regular );
}
}
}
Step 3: Add project references to other Windows Forms projects. After compilation, you can see the generated custom control in the toolbox.
URL: http://greatverve.cnblogs.com/archive/2012/02/16/user-control-Inherit.html
See msdn: http://msdn.microsoft.com/zh-cn/library/5h0k2e6x (V = vs.80). aspxhttp: // blog.csdn.net/yysyangyangyangshan/article/details/7078471