How to work with delegates and event handler for user control
Asked Answered
A

3

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.

Albertinealbertite answered 27/5, 2010 at 10:8 Comment(0)
S
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.
Sophi answered 27/5, 2010 at 10:13 Comment(4)
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 ?Albertinealbertite
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.Sophi
Why can't i write public event int funName; what actually is accepted by events ?Albertinealbertite
You need a handler. You could just change the delegate (which is the handler) to public delegate void FunName(int i);Sophi
A
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
    }
  }
}
Ankylosis answered 12/4, 2012 at 16:55 Comment(3)
Don't you need a definition for innerButton somewhere? Is this missing a piece?Luciusluck
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.Saffren
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.Aberrant
A
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
  }
}
Aggiornamento answered 28/8, 2018 at 20:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.