Reputation: 491
I want to get the clicked event of a Button
which is in my UserControl
and I am adding that UserControl
dynamically in my form. I want the event to be raised in the Form
in which i am adding the UserControl
. Please if anyone could suggest me proper way then it will be really very helpful.
Upvotes: 0
Views: 1957
Reputation: 4269
I guess you're using Winforms referring to your title.
What you can do it to forward your Click
event.
So in the ctor of your UserControl
public class MyUserControl
{
public event EventHandler MyClick;
private void OnMyClick()
{
if (this.MyClick != null)
this.MyClick(this, EventArgs.Empty);
}
public MyUserControl()
{
this.Click += (sender, e) => this.OnMyClick();
}
}
Upvotes: 2
Reputation: 36517
Add your own event to your custom user control.
Inside your customer user control, once you add the button, also attach your (internal) event handler that will then raise your own public event with some way to tell the event handler which button has been clicked (you'll most likely need your own delegate here).
Once that's done, your form can add its own event handler just like you add one to the standard controls.
Rereading your question, this might not be the exact structure (the button is fixed, but the user control is dynamically added?). In any way, it should be almost the same, it just differs where/when you add the event handler upon creation.
With one static button it's a lot easier to be done - assuming you're using Windows Forms:
In your custom user control:
public event EventHandler ButtonClicked; // this could be named differently obviously
...
public void Button_OnClick(object sender, EventArgs e) // this is the standard "on button click" event handler created using the form editor
{
if (ButtonClicked != null)
ButtonClicked(this, EventArgs.Empty);
}
In your form:
// create a new user control and add the event
MyControl ctl = new MyControl();
Controls.Add(ctl);
ctl.ButtonClicked += new EventHandler(Form_OnUserControlButtonClicked); // name of the event handler in your form that's called once you click the button
...
private void Form_OnUserControlbuttonClicked(object sender EventArgs e)
{
// do whatever should happen once you click the button
}
Upvotes: 0
Reputation: 57220
You need to expose the event in your user control, then subscribe it when you add the user control to the form. e.g.:
public partial MyUserControl:Control
{
public event EventHandler ButtonClicked;
private void myButtonClick(object sender, EventArgs e)
{
if (this.ButtonClicked != null)
this.ButtonClicked(this, EventArgs.Empty);
}
}
public partial MyForm:Form
{
private void MethodWhereYouAddTheUserControl()
{
var myUC = new MyUserControl();
myUC += myUC_ButtonClicked;
// code where you add myUC to the form...
}
void myUC_ButtonClicked(object sender, EventArgs e)
{
// called when the button is clicked
}
}
Upvotes: 3
Reputation: 8786
when you are adding your usercontrol
to your form
, register the click event (if it is public
) usercontrol.button.Click += new EventHandler(usercontrolButton_Click);
register the button's Click
event within your usercontrol
Upvotes: 0