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

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 6 Years Ago by nick.crane: n/a

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