Greetings.

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.

Recommended Answers

All 3 Replies

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.)

thanks, works nicely.

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 :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.