8

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 need to remove a row from a datatable on the Click event of that button.

The problem is that how do I capture that event in my form. Currently it goes in that user control's btn click event defination.

Shantanu Gupta
  • 20,688
  • 54
  • 182
  • 286

3 Answers3

21

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.
djdd87
  • 67,346
  • 27
  • 156
  • 195
  • Nice nick :), Could you please xplain little bit abt it. I dont know much about delegates, events and Interfaces. How does it works. How will it recognize that a button has been clicked ? – Shantanu Gupta May 27 '10 at 10:18
  • 1
    Basically, a delegate is just points to a specified functions. Delegates are multicast, which means they can simultaneously point to multiple methods. I.e. calling the delegate once will fire off all the attached methods. No point me explaining interfaces as they're irrelevant in this question, but there's plenty of questions about them on SO if you do a search. – djdd87 May 27 '10 at 10:22
  • Why can't i write public event int funName; what actually is accepted by events ? – Shantanu Gupta May 27 '10 at 10:27
  • You need a handler. You could just change the delegate (which is the handler) to public delegate void FunName(int i); – djdd87 May 27 '10 at 10:41
6

It's not necessary to declare a new delegate. In your user control:

public class MyControl : UserControl
{
  public event EventHandler InnerButtonClick;
  public MyControl()
  {
    InitializeComponent();
    innerButton.Click += new EventHandler(innerButton_Click);
  }
  private void innerButton_Click(object sender, EventArgs e)
  {
    if (InnerButtonClick != null)
    {
      InnerButtonClick(this, e); // or possibly InnerButtonClick(innerButton, e); depending on what you want the sender to be
    }
  }
}
ChéDon
  • 131
  • 2
  • 3
  • Don't you need a definition for innerButton somewhere? Is this missing a piece? – Alex Amato Sep 08 '13 at 22:07
  • 1
    InnerButtonClick is defined by this line: "public event EventHandler InnerButtonClick;" If you don't need a specialized event handler, there's no need to redefine the base one. – ChéDon Nov 08 '13 at 19:01
  • This and the example above were exactly what I wanted. Completing the example of hooking up the Event and showing how a client can subscribe would help. Perhaps also add retrieving a public property on the user control vs. custom EventArgs, but the above is a commonly used situation. – beanmf May 06 '18 at 20:29
0

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
  }
}
M Granja
  • 845
  • 8
  • 26