Search code examples
c#winformsmouseeventmouseenter

MouseEnter & MouseLeave objectname


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;

Solution

  • var panel = sender as Control;
    var thePanelName = panel.Name;