Answer the question
In order to leave comments, you need to log in
How to determine which Control was clicked if it was created by code?
Hello, I am creating software that will dynamically add controls to a form.
Here is an example of creating a control:
Label label = new Label();
label.ForeColor = Color.LightYellow;
label.BackColor = Color.Transparent;
label.AutoSize = false;
label.Size = textSize;
label.TextAlign = ContentAlignment.MiddleCenter;
label.Text = _login;
backPanel.Controls.Add(label);
Answer the question
In order to leave comments, you need to log in
did you refer to the sender object when creating the button's Click method ?
private void button1_Click(object sender, EventArgs e)
{
// тут какой-то код
}
private void label_Click(object sender, EventArgs e)
{
Label lb = sender as Label;
lb.Content = "какой-то текст";
// или
int x;
switch((sender as Label).Name)
{
case "label": x = 0; break;
case "label2": x = 1; break;
case "label3": x = 2; break;
default: x = -1; break;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question