简体   繁体   English

在C#中的UserControl中创建一个ButtonClick事件

[英]Create an ButtonClick Event in UserControl in C#

I create the ButtonClick event in the UserControl below and want to handle it in a Form, but I get an error that says 'The name UserControl1.ButtonClick' does not exist in the current context. 我在下面的UserControl中创建了ButtonClick事件,并希望在Form中进行处理,但是出现一个错误,提示“名称UserControl1.ButtonClick”在当前上下文中不存在。

public partial class UserControl1 : UserControl
{
        public UserControl1()
    {
        InitializeComponent();
    }
        public event EventHandler ButtonClick;


    private void btnUpdate_Click(object sender, EventArgs e)
    {
        if (this.ButtonClick != null)
            this.ButtonClick(this, e);
    }

Form: 形成:

        UserControl1.ButtonClick += new EventHandler(UserControl_ButtonClick);



    protected void UserControl_ButtonClick(object sender, EventArgs e)
    {
      //handle the event
    }

Form: 形成:

public PatientNumber()
        {
            InitializeComponent();
            userControl11.updateButton.Click += button_Click;
        }

        protected void button_Click(object sender, EventArgs e)
        {
         //handle the event
        }

UserControl: 用户控件:

public Button btnUpdatee
{
    get
    {
        return btnUpdate;
    }
    set
    {
        btnUpdate = value;
    }
}

This code worked. 此代码有效。 Thank you 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM