0

I'm trying to develop a system where the user can scroll through a flowLayoutPanel, which contains a list on panels (which are created dynamically). Each panel has a pictureBox and two labels in it. When the user hovers the mouse over a panel, I want the background image of the panel to change.

So far I have made these methods:

    private void pnlMouseHover(object sender, EventArgs e)
    {
        Panel panel1 = sender as Panel;
        panel1.BackgroundImage = STUDIO2.Properties.Resources.buttonbackgroundmouseover;
    }
    private void pnlMouseLeave(object sender, EventArgs e)
    {
        Panel panel1 = sender as Panel;
        panel1.BackgroundImage = STUDIO2.Properties.Resources.buttonbackground;
    }

These work just fine, but when running the system these methods only run when the mouse is hovering over part of the panel which is not covered by a label or pictureBox.

How can I change this so when the mouse is hovering over the areas inside the panel, which are covered by a label or pictureBox, the background image changes?

Andrew
  • 39
  • 1
  • 2
  • 4
  • does this help - http://stackoverflow.com/questions/1161280/parent-control-mouse-enter-leave-events-with-child-controls – Carbine Feb 17 '16 at 17:59
  • Yeah that seems like it will be useful, thank you! – Andrew Feb 17 '16 at 18:02
  • looks like the child control swallows the mouse over event. You just need to loop through the child control and prevent them from swallowing it. – Carbine Feb 17 '16 at 18:03

1 Answers1

1

The possibilities of solving this are endless. Here's one way.

Enter & Leave

Change your panel_Hover event to panel_Enter so that the event is not continually fired.

Conditional Exit

Check the position of the pointer before removing the picture.

private void pnlMouseLeave(object sender, EventArgs e)
{
    Panel panel1 = sender as Panel; // ← Your code

    Point mousePosition = PointToClient(Control.MousePosition);
    bool hasPointerInside = panel1.ClientRectangle.Contains(mousePosition);

    if (!hasPointerInside)
    {
        // Your code ↓
        panel1.BackgroundImage = STUDIO2.Properties.Resources.buttonbackground;

    }
}
Tyler Pantuso
  • 835
  • 8
  • 16