I am trying to implement an event handler for my user control that triggers a click whenever any control inside the user control or the user control itself is clicked.
public event EventHandler ClickCard
{
add
{
base.Click += value;
foreach (Control control in GetAll(this, typeof(Control)))
{
control.Click += value;
}
}
remove
{
base.Click -= value;
foreach (Control control in GetAll(this, typeof(Control)))
{
control.Click -= value;
}
}
}
public IEnumerable<Control> GetAll(Control control, Type type)
{
var controls = control.Controls.Cast<Control>();
return controls.SelectMany(ctrl => GetAll(ctrl, type))
.Concat(controls)
.Where(c => c.GetType() == type);
}
I modified the code given here to bind all the nested controls. This is how I am binding the event on which this user control is used:
private void feedbackCard1_ClickCard_1(object sender, EventArgs e)
{
MessageBox.Show("Thank You!");
}
But the click is not firing on clicking any of the controls inside the user control or the user control itself.