0

Hi ! I have one Windows Form. On it i put 4 labels and every label it have Click event.
I need to understand who of labels is Clicked ?

Example:

label1.Click += new EventHandler(label_Click);
        label2.Click += new EventHandler(label_Click);
        label3.Click += new EventHandler(label_Click);
        label4.Click += new EventHandler(label_Click);

      private void label_Click(object sender, EventArgs e)
      {
          // i need to understand who of labels is Clicked ???
      }

Is this possible ??? Sorry for my English...

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by destruct0
0

The sender object is the object that was clicked.
You need to cast it to the Label control class to use it.
e.g.

private void label_Click(object sender, EventArgs e)
      {
          Label label = sender as Label;
          if (label != null)
          {
              // do something with label
              label.Text = "You clicked me!";
          }
      }

[Edit] Actually casting would be Label label = (Label)sender; but using the as operator is safer as this tests the type of object first and returns null if it does not match, whereas the simple cast would crash your code if the sender object was not a Label or is null.

Edited by nick.crane: n/a

0

:) Thank you very much ! It work perfect. I`ll mark this Thread as Solved !

This question has already been answered. 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.