简体   繁体   中英

How can I make any label's color change, without repeating the same color change line for each label?

I have a form with 3 labels drawn on.

And I have this code

  private void Form1_Load(object sender, EventArgs e)
        {
            //drawn 3 labels.

            List<Label> lstlabels = new List<Label>() { label1, label2, label3 };

            int cnt = lstlabels.Count;
            for (int i = 0; i < cnt; i++) lstlabels[i].Click += (object se, EventArgs ee) => { if (this.BackColor == SystemColors.Control) this.BackColor = Color.Yellow; };

        }

It doesn't do what I want it to do.. and I know why, but I don't know how to make it do what I want it to do.

What I would like, is that when I click a label, it goes yellow.

What the code is doing, is that when I click a label, the whole form goes yellow.

I know why it's doing that. It's doing that because 'this' is referring to the form rather than the label that was clicked.

I would like to change 'this' to something that refers to the label that was clicked. Like not necessarily change 'this', but use something else instead of this, that would do that, but I have no idea how.

I can try changing 'this' to lstlabels[i] but that won't work either.. I understand why that won't work either.. it gives an index out of range error. I understand why. I know that at the time a label is clicked, i==3 and so i is out of bounds and gives no indication of what label was clicked. So I just don't know how to do this.

I could do

    label1.Click += (object se, EventArgs ee) => { if (label1.BackColor == SystemColors.Control) label1.BackColor = Color.Yellow; };
    label2.Click += (object se, EventArgs ee) => { if (label2.BackColor == SystemColors.Control) label2.BackColor = Color.Yellow; };
    label3.Click += (object se, EventArgs ee) => { if (label3.BackColor == SystemColors.Control) label3.BackColor = Color.Yellow; };

or

        lstlabels[0].Click += (object se, EventArgs ee) => { if (label1.BackColor == SystemColors.Control) label1.BackColor = Color.Yellow; };
        lstlabels[1].Click += (object se, EventArgs ee) => { if (label2.BackColor == SystemColors.Control) label2.BackColor = Color.Yellow; };
        lstlabels[2].Click += (object se, EventArgs ee) => { if (label3.BackColor == SystemColors.Control) label3.BackColor = Color.Yellow; };

And if there are 25 labels then 25 lines like that..

But I guess that it should really be one or two lines.

And the labels might not be label1 and label2 and label3, they might be lblBob, lblJohn, lblAndy

I think it should really be one or two lines.. and not more lines when more labels, but i'm not sure how 'cos I can't see how to do it in a loop!

First, for convenience, create a seperate method for the event. Second, when you use the keyword this you refer to the form, and not to the sender label. In our new method, the sender argument is that label. Also, if taht's all your code is supposed to do, there is no need to check the current background color.

private void Label_Clicked(object sender, EventArgs e)
{
     Label clickedLabel = (Label)sender;
     clickedLabel.BackColor = Color.Yellow;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM