3

I want to add MouseOver and MouseLeave events to dynamical created panels in a flowLayoutPanel.

I added all panels in a list named "panels" and they are accessible with "panels[index]".

Now I want to dynamical add a MouseOver and MouseLeave event to each panel. I thought it could be possible to get the panelname the Mouse is over and use just one method for each event and identify the panel the mouse is over with its panelname (panel.Name) but I found nothing in "sender".

Is there a way to do this?

My code:

//Method
private void PanelsMouseEnter(object sender, EventArgs e)
{
    var panel = sender as Control;
    foreach (Control control in this.fLpKoerper.Controls)
    {
        if (control.Name == panel.Name)
        {
            foreach (Panel panels in panelsKoerper)
            {
                if (panels.Name == panel.Name)
                    panels.BackColor = Color.DarkGray;
            }
        }
    }  
}

//Event
panelsKoerper[y].MouseEnter += PanelsMouseEnter;
Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
kryptex
  • 33
  • 3

2 Answers2

1
var panel = sender as Control;
var thePanelName = panel.Name;
Adam Barney
  • 2,377
  • 3
  • 18
  • 24
0

I believe you can generate one mouseover event for a control, copy that event method name and then paste it into another controls mouseover event box and that should work

So you would have this event

private void label1_MouseHover(object sender, EventArgs e)
{
    //Code...
}

and then you could put 'label1_MouseHover' in any controls mouseover event

Mungoid
  • 565
  • 5
  • 16
  • thank you, too. This is another idea I had before, but since the amount of panels comes from a jagged array from a dll, they´re created on runtime. – kryptex Nov 03 '11 at 20:07