How to work with delegates and event handler for user control

前端 未结 3 819
说谎
说谎 2020-12-16 04:42

I have created a user control that contains a button. I am using this control on my winform which will be loaded at run time after fetching data from database.

Now I

3条回答
  •  轮回少年
    2020-12-16 05:00

    You can create your own delegate event by doing the following within your user control:

    public event UserControlClickHandler InnerButtonClick;
    public delegate void UserControlClickHandler (object sender, EventArgs e);
    

    You call the event from your handler using the following:

    protected void YourButton_Click(object sender, EventArgs e)
    {
       if (this.InnerButtonClick != null)
       {
          this.InnerButtonClick(sender, e);
       }
    }
    

    Then you can hook into the event using

    UserControl.InnerButtonClick+= // Etc.
    

提交回复
热议问题