User-defined control

User control development
1. Classification

组合控件: 在原有控件基础上,根据需要进行组合而形成一个新的控件  UserControl
 	
扩展控件: 在现在基础上,对它进行扩展:继承自原有控件,添加或扩展原有控件的性能
 
 自定义控件: 派生Control类,绘制全部由用户定义 

2. Combination control
①Visual appearance
②Add a new property to the application form, modify the property value, you can see the changes immediately
③ Define a custom event
Load event Application form: Load event----loading sequence: application window Body: Load event ---- User control Load event to create a new user- defined
control
Insert picture description here

//添加属性
private string _lblText;	//Label的文本
public string LblText
{
    
    
    get {
    
     return _lblText; }
    set
    {
    
    
        _lblText = value;
        lblContent.Text = _lblText;	//即刻关联
    }
}
private string _btnText;	//Button的文本
public string BtnText
{
    
    
    get {
    
     return _btnText; }
    set
    {
    
    
        _btnText = value;
        btnConfirm.Text = _btnText;
    }
}
//事件 调用:只能在自己内部调用 
public event Action<object, EventArgs> ShowMsg;
private void btnConfirm_Click(object sender, EventArgs e)
{
    
    
    if (ShowMsg != null)
        ShowMsg(sender, e);
}

Use of custom controls
Insert picture description here

private void FrmUserControl_Load(object sender, EventArgs e)
{
    
    
    userButton1.LblText = "eeee";
    userButton1.BtnText = "OK";
}
//事件
private void userButton1_ShowMsg(object sender, EventArgs e)
{
    
    
    MessageBox.Show("aaaa");
}

Insert picture description here

3. The extended control
inherits from the class provided by the original control

New class library ButtonEx control

 public class ButtonEx:Button
{
    
    
    private string _btnText;	//Button的文本
    public string BtnText
    {
    
    
        get {
    
     return _btnText; }
        set
        {
    
    
            _btnText = value;
            Text = _btnText; 	//BtnText属性和Text属性关联
        }
    }
   //重写事件
   protected override void OnClick(EventArgs e)
   {
    
    
       this.ForeColor = Color.Red;
       base.OnClick(e);
   }
}

Use of custom controls
Insert picture description here

private void buttonEx1_Click(object sender, EventArgs e)
{
    
    
     MessageBox.Show("OK");
 }

Guess you like

Origin blog.csdn.net/asdasd1fdsyrt/article/details/114236522