How to work with delegates and event handler for user control

前端 未结 3 816
说谎
说谎 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条回答
  •  -上瘾入骨i
    2020-12-16 05:02

    Just modernizing ChéDon's answer, here is how you can do it in 2018:

    public class MyControl : UserControl
    {
      public event EventHandler InnerButtonClick;
    
      public MyControl()
      {
        InitializeComponent();
        innerButton.Click += innerButton_Click;
      }
    
      private void innerButton_Click(object sender, EventArgs e)
      {
          InnerButtonClick?.Invoke(this, e);
          //or
          InnerButtonClick?.Invoke(innerButton, e); 
          //depending on what you want the sender to be
      }
    }
    

提交回复
热议问题