i have a program which dynamically creates panels and stores them into a panel array. each panel has added mouse click action event. what i need to know is how to determine on which panel i have clicked on? thanks.

7 Years
Discussion Span
Last Post by Geekitygeek

Once you've got the control on the form in your code add an event handler to it programatically.
For example: this.panel1.Click += new System.EventHandler(this.panel1_Click); (and of course you need to code the panel1_Click method just like you would have done by clicking on the properties window).

Then just use the panel1.Name or panel1.Tag members to set a name to use in your program and display that in a messagebox or whatever you had planned to do.)

Edited by jonsca: n/a


Also, the standard event handler has two parameters, sender and eventArgs. sender will contain a reference to the control that fired the event. You need to cast it back to its correct type to access its members though:

private void Panel1_Click(object sender, EventArgs e)
    Panel clicked = (Panel)sender; //gets the panel you clicked on
    Control clicked = (Control)sender; //use this if you can't be sure of the control type
    string Name = sender.Name;

Remember to mark the thread as sovled if your question has been answered :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.