user2194683
user2194683

Reputation: 135

Event action of a specific label from an array of labels c#

I am working with an array of labels and I don't really know how to add a click action to all of them. For example if the user clicks on arrow[i] that arrow should display something. I've also searched on the internet and I haven't found anything useful.

Here is my code:

    Label[] _arr = new Label[4];
    private void button1_Click(object sender, EventArgs e)
    {
        for(int i=0;i<4;i++){

    _arr[i ] = new Label();
    _arr[i ].Text = ""+i;
    _arr[i ].Size = new Size(50,50);
    _arr[i ].Location = new Point(i*50,i*50);
    this.Controls.Add(_arr[i]);
}

}

Thank you for your help.

Upvotes: 0

Views: 321

Answers (3)

Sayse
Sayse

Reputation: 43300

When defining your label, include an event as such

_arr[i].Click += label1_Click;

Then if your using visual studio, you should get help creating the actual method by clicking tab at certain points during writing the above line. otherwise you need to make an event method your self such as

    void label1_Click(object sender, EventArgs e)
    {
        //do stuff
    }

Upvotes: 1

Prabhu Murthy
Prabhu Murthy

Reputation: 9261

 _arr[i] = new Label();   
 _arr[i].Click += (s, e) => MessageBox.Show("Message"); 

Upvotes: 0

Johan Nordli
Johan Nordli

Reputation: 1230

_arr[i ].Click += delegate { what you want to happend on click };

Upvotes: 0

Related Questions