关于在c#中创建用户控件后,winform应用程序在调用中无法通过点击用户控件的子控件为其自动添加事件代码的问题

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

测试代码

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

设计器文件 UserControl1.Designer.cs

namespace WindowsControlLibrary1
{
    partial class UserControl1
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;


        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }


        #region 组件设计器生成的代码


        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(63, 69);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(70, 114);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(41, 12);
            this.label1.TabIndex = 1;
            this.label1.Text = "label1";
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(63, 151);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(100, 21);
            this.textBox1.TabIndex = 2;
            // 
            // UserControl1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.AutoValidate = System.Windows.Forms.AutoValidate.EnableAllowFocusChange;
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.button1);
            this.Name = "UserControl1";
            this.Size = new System.Drawing.Size(269, 232);
            this.ResumeLayout(false);
            this.PerformLayout();


        }

        #endregion


        public System.Windows.Forms.Button button1;
        public System.Windows.Forms.Label label1;
        public System.Windows.Forms.TextBox textBox1;
    }
}


代码文件UserControl1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace WindowsControlLibrary1
{
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }
    }
}


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

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

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

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

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void userControl11_button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("userControl11 button1 Click");
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.userControl11.button1.Click += userControl11_button1_Click;
        }
    }
}

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





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值