Saad Amrani
Saad Amrani

Reputation: 515

How to check if a dynamically created Label is clicked and Change its colors?

To be honest I don't really think there should be a big mess/problem with my code, but somehow the colors are not being changed when I click on a label!

What I was trying to achieve there was, if the user clicks Male then it changes color to green and the other Label, Female, changes back to red, and the opposite.

for (int i = 0; i < 2; i++)
{
    Label RB = new Label();
    RB.Location = new Point(x,y);
    RB.Width = 95;
    RB.Text = LabelsText[4].Split('-')[i];
    RB.BackColor = Color.PaleVioletRed;
    RB.TextAlign = ContentAlignment.MiddleCenter;
    RB.Font = new Font(family.Families[0], 11.0f, FontStyle.Bold);
    RB.ForeColor = Color.AntiqueWhite;
    RB.Name = txtBoxNames[4].Split('-')[i];
    RB.MouseClick += (s, ev) =>
    {
        if (((Label)(s)).Name == "isMale")
        {
            ((Label)(s)).BackColor = Color.GreenYellow;
            ((Label)(s)).ForeColor = Color.Black;

            foreach (Control ct in this.Controls)
            {
                if (ct is Label)
                {
                    if (((Label)(ct)).Name == "isFemale")
                    {
                        ((Label)(s)).BackColor = Color.PaleVioletRed;
                        ((Label)(s)).ForeColor = Color.AntiqueWhite;
                    }
                }
            }
            this.Refresh();
            this.Update();
        }
        else if(((Label)(s)).Name == "isFemale")
        {
            ((Label)(s)).BackColor = Color.GreenYellow;
            ((Label)(s)).ForeColor = Color.Black;

            foreach (Control ct in this.Controls)
            {
                if (ct is Label)
                {
                    if (((Label)(ct)).Name == "isMale")
                    {
                        ((Label)(s)).BackColor = Color.PaleVioletRed;
                        ((Label)(s)).ForeColor = Color.AntiqueWhite;
                    }
                }
            }
        }
    };
    this.Controls.Add(RB);
    x += RB.Width + 10 ;
}

Upvotes: 0

Views: 102

Answers (2)

Jimi
Jimi

Reputation: 32288

Since you're adding the two controls to the this.Controls collection, you can access the other gender using its Name: this.Controls["ButtonName"]:

You can also cast object s to Control to simplify the syntax:

var lbl = (s as Control);

Some more cuts here and there.


for (int i = 0; i < 2; i++)
{
    Label label = new Label();
    label.Width = 95;
    label.Location = new Point(x + (i * label.Width) + (i * 10), y);
    label.BackColor = Color.PaleVioletRed;
    label.ForeColor = Color.AntiqueWhite;
    label.TextAlign = ContentAlignment.MiddleCenter;
    label.Name = "isMale-isFemale".Split('-')[i];
    label.Text = label.Name;
    label.MouseClick += (s, ev) =>
    {
        var lbl = (s as Control);
        string other = lbl.Name.Equals("isFemale") ? "isMale" : "isFemale";
        lbl.BackColor = Color.GreenYellow;
        lbl.ForeColor = Color.Black;
        var ctl = this.Controls[other];
        if (ctl != null)
        {
            ctl.BackColor = Color.PaleVioletRed;
            ctl.ForeColor = Color.AntiqueWhite;
        }
    };
    this.Controls.Add(label);
}

Upvotes: 1

LarsTech
LarsTech

Reputation: 81675

In your for-each loops, you are changing the "sender" control back to PaleVioletRed instead of the label you found:

Change this:

if (((Label)(ct)).Name == "isFemale") {
  ((Label)(s)).BackColor = Color.PaleVioletRed;
  ((Label)(s)).ForeColor = Color.AntiqueWhite;
}

to this:

if (((Label)(ct)).Name == "isFemale") {
  ((Label)(ct)).BackColor = Color.PaleVioletRed;
  ((Label)(ct)).ForeColor = Color.AntiqueWhite;
}

Upvotes: 1

Related Questions