FrostyFire
FrostyFire

Reputation: 3258

how to tell which panel fired the click event

i have an array of panel controls that are placed in various place programatically in my form. Below you can see that as I loop through and increase I, all the panels are registered with the same event handler. This is fine with me, but I cant find a way to tell which panel fired the event, in the event handler code. I tried using senderbut I cant seem to figure out how to use it. So my question is how can I tell which panel fired the event, in the event handler code

     Panels[i].Click += new EventHandler(AllPanels_Click);

  void AllPanels_Click(object sender, EventArgs e)
        {
           //need logic code here!
        }

Upvotes: 2

Views: 1773

Answers (2)

d.moncada
d.moncada

Reputation: 17402

void Form1_Click(object sender, EventArgs e)
{
    var panel = sender as Panel;
    if (null != panel)
    {
        if (panel.Name.equals("Panel1"))
        {
             .. ...
        }
    }
}

Upvotes: 3

gilly3
gilly3

Reputation: 91657

Cast sender to a Panel first:

void Form1_Click(object sender, EventArgs e)
{
    Panel clickedPanel = sender as Panel;
    if (clickedPanel != null)
    {
        // do something with clickedPanel
    }
}

Upvotes: 4

Related Questions