views:

161

answers:

1

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.

+2  A: 

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.
GenericTypeTea
@Generic: 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
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.
GenericTypeTea
@Generic: Why can't i write public event int funName; what actually is accepted by events ?
Shantanu Gupta
You need a handler. You could just change the delegate (which is the handler) to public delegate void FunName(int i);
GenericTypeTea