c#中创建用户控件后,子控件为其自动添加事件代码

2019/12/13

由于开发中业务信息的不确定性,今天制作了个usercontrol组件,该用户控件中添加了若干个子控件(如button、textbox、label),但是在winform应用程序中调用时,发现无法对其子控件(如button)进行单击自动生成button click事件代码的问题,通过google搜索了下,大致找到了解决方法,现在记录下来,仅作为开发笔记,如浏览本帖的人员有其他更好的方法,或者对其原理进行阐述的,可以留言,谢谢各位

测试代码

1.首先先创建usercontrol用户控件,具体如何创建各位可去进行google搜索,或者查找相关书籍教程

设计器文件 UserControl1.Designer.cs


[csharp] view plaincopy?
  1. namespace WindowsControlLibrary1  
  2. {  
  3.     partial class UserControl1  
  4.     {  
  5.         /// <summary>  
  6.         /// 必需的设计器变量。  
  7.         /// </summary>  
  8.         private System.ComponentModel.IContainer components = null;  
  9.   
  10.   
  11.         /// <summary>  
  12.         /// 清理所有正在使用的资源。  
  13.         /// </summary>  
  14.         /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>  
  15.         protected override void Dispose(bool disposing)  
  16.         {  
  17.             if (disposing && (components != null))  
  18.             {  
  19.                 components.Dispose();  
  20.             }  
  21.             base.Dispose(disposing);  
  22.         }  
  23.  
  24.  
  25.         #region 组件设计器生成的代码  
  26.   
  27.   
  28.         /// <summary>  
  29.         /// 设计器支持所需的方法 - 不要  
  30.         /// 使用代码编辑器修改此方法的内容。  
  31.         /// </summary>  
  32.         private void InitializeComponent()  
  33.         {  
  34.             this.button1 = new System.Windows.Forms.Button();  
  35.             this.label1 = new System.Windows.Forms.Label();  
  36.             this.textBox1 = new System.Windows.Forms.TextBox();  
  37.             this.SuspendLayout();  
  38.             //   
  39.             // button1  
  40.             //   
  41.             this.button1.Location = new System.Drawing.Point(63, 69);  
  42.             this.button1.Name = "button1";  
  43.             this.button1.Size = new System.Drawing.Size(75, 23);  
  44.             this.button1.TabIndex = 0;  
  45.             this.button1.Text = "button1";  
  46.             this.button1.UseVisualStyleBackColor = true;  
  47.             //   
  48.             // label1  
  49.             //   
  50.             this.label1.AutoSize = true;  
  51.             this.label1.Location = new System.Drawing.Point(70, 114);  
  52.             this.label1.Name = "label1";  
  53.             this.label1.Size = new System.Drawing.Size(41, 12);  
  54.             this.label1.TabIndex = 1;  
  55.             this.label1.Text = "label1";  
  56.             //   
  57.             // textBox1  
  58.             //   
  59.             this.textBox1.Location = new System.Drawing.Point(63, 151);  
  60.             this.textBox1.Name = "textBox1";  
  61.             this.textBox1.Size = new System.Drawing.Size(100, 21);  
  62.             this.textBox1.TabIndex = 2;  
  63.             //   
  64.             // UserControl1  
  65.             //   
  66.             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);  
  67.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;  
  68.             this.AutoValidate = System.Windows.Forms.AutoValidate.EnableAllowFocusChange;  
  69.             this.Controls.Add(this.textBox1);  
  70.             this.Controls.Add(this.label1);  
  71.             this.Controls.Add(this.button1);  
  72.             this.Name = "UserControl1";  
  73.             this.Size = new System.Drawing.Size(269, 232);  
  74.             this.ResumeLayout(false);  
  75.             this.PerformLayout();  
  76.   
  77.   
  78.         }  
  79.  
  80.         #endregion  
  81.   
  82.   
  83.         public System.Windows.Forms.Button button1;  
  84.         public System.Windows.Forms.Label label1;  
  85.         public System.Windows.Forms.TextBox textBox1;  
  86.     }  
  87. }  


代码文件UserControl1.cs


[csharp] view plaincopy?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Drawing;  
  5. using System.Data;  
  6. using System.Text;  
  7. using System.Windows.Forms;  
  8.   
  9. namespace WindowsControlLibrary1  
  10. {  
  11.     public partial class UserControl1 : UserControl  
  12.     {  
  13.         public UserControl1()  
  14.         {  
  15.             InitializeComponent();  
  16.         }  
  17.     }  
  18. }  


2.需要将usercontrol中的子控件(如 button)属性中的Modifiers中的值改为Public

3.进行编译,编译成功将生成的usercontrol dll通过鼠标拖动工具箱中。

4.在该解决方案中,添加winform应用程序项目,然后将刚刚添加到工具箱中的usercontrol 控件拖动到winform窗体中

5.开始在winform应用程序中编写usercontrol 子控件(button)的单击事件代码


[csharp] view plaincopy?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Text;  
  7. using System.Windows.Forms;  
  8.   
  9. namespace WindowsApplication1  
  10. {  
  11.     public partial class Form1 : Form  
  12.     {  
  13.         public Form1()  
  14.         {  
  15.             InitializeComponent();  
  16.         }  
  17.   
  18.         private void userControl11_button1_Click(object sender, EventArgs e)  
  19.         {  
  20.             MessageBox.Show("userControl11 button1 Click");  
  21.         }  
  22.   
  23.         private void Form1_Load(object sender, EventArgs e)  
  24.         {  
  25.             this.userControl11.button1.Click += userControl11_button1_Click;  
  26.         }  
  27.     }  
  28. }  

6.编译winform应用程序,运行后点击usercontrol中的button即可看到效果。