Multiple Controls Click Events Handled by a Single Event

I am creating a basic winform application that has many pictures with click events. Click events should use the name of the image window that was clicked to execute the rest of the code. I do not want to create unique click events for all graphic boxes. I was hoping there would be an easy way to get this information, for example using the sender parameter or event arguments.

+1
source share
1 answer

You add a one-click event handler for all PictureBoxes:

pic1.Click += PictureBoxClick;
pic2.Click += PictureBoxClick;

PictureBox, , , :

private void PictureBoxClick(object sender, EventArgs e)
{
    var picBoxName = ((PictureBox)sender).Name;
}

:

pic1.Click -= PictureBoxClick;
pic2.Click -= PictureBoxClick;
+1

Source: https://habr.com/ru/post/1662763/


All Articles