I have a custom control that I created for my project. In this control there are several child controls like a Label, a PictureBox, and a LinkLabel. Other then the LinkLabel, I want the mouse over event currently on the parent control and have the control respond to the mouse over. The background color changes when you hover over the control, but the background color doesn't change when over a child control; this is because there is no MouseEnter and MouseLeave events on the child control. I solved this issue by added the parent controls delegate methods to the child controls. The problem remains that the click event also is ignored over the child controls when I've subscribed to the click event on my parent control. I can subscribe to each individual child control, but how do I force the click event of the parent control? The term I've found by searching is Event Bubbling, but this seems to only apply to ASP.NET technologies and frameworks. Any suggestions?
Your description leads me to believe that you want both your child and your parent controls to respond to a click on the child control.
If I understand your question correctly, I'd suggest subscribing to your child controls' click events and, in those event handlers, calling some common method that manipulates the state of the parent UserControl in the manner that you desire (e.g., changing the background color).
It seems that someone else solved it for me. A friend explained that C# controls support a method called InvokeOnClick(Control, EventArgs);
I added my event delegate methods to each child control, and for the click event, I created a single method for each child control to use. This in turn calls InvokeOnClick(this, new EventArgs());
private void Control_Click(object sender, EventArgs e)
{
// this is the parent control.
InvokeOnClick(this, new EventArgs());
}
private void IFLVControl_MouseEnter(object sender, EventArgs e)
{
this.BackColor = Color.DarkGray;
}
private void IFLVControl_MouseLeave(object sender, EventArgs e)
{
this.BackColor = Color.White;
}
Peter Rilling over at CodeProject has some simple and effective code to do event bubbling/broadcasting in winforms (and C#). It's really easy to use.