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

Recommended Answers

All 2 Replies

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.

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

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.