Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have 3 option to change the color of label

1 - Red
2 - Green
3 - yallow

I created 100 labels.
I have table contains the name of the lable and the value of option

NAME OF LABLE OPTION
A1 1
A2 2
A3 1
A4 3
A8 1
B1 1
B3 2

How can I change the colors of labels? or there is other way
Posted
Comments
Sergey Alexandrovich Kryukov 11-Aug-15 13:24pm    
Label? Which one? Full type name, please. And what's the problem? All Label types allow custom color, background and foreground. What's wrong with reading original MSDN documentation?
—SA
Member 11232188 11-Aug-15 13:37pm    
Should I change the back color for every label manually. "100 label"
Sergey Alexandrovich Kryukov 11-Aug-15 13:43pm    
What does it mean, "manually"? Again, what's the problem?
(You did not answer my question, but I can see that you probably work with System.Windows.Forms.Label. However, who will want to talk with you if you don't answer important questions?)
What have you tried so far?
—SA
Member 11232188 11-Aug-15 13:32pm    
for example label "A1" ---- RED
"A2" ---- GREEN
"A3" ---- RED
Member 11232188 11-Aug-15 13:39pm    
protected void btnUpadte_Click(object sender, EventArgs e)
{

ParkingLot p = new ParkingLot();
int x = p.GetStatusLot("A1");
if (x == 1)
{
lblA1.BackColor = System.Drawing.Color.Red;
}
else if (x == 2)
lblA1.BackColor = System.Drawing.Color.Green;
else if (x == 3)
lblA1.BackColor = System.Drawing.Color.Orange;
}

1 solution

Try:
C#
foreach (Control c in Controls)
    {
    Label l = c as Label;
    if (l != null)
        {
        l.ForeColor = Color.Red;
        l.BackColor = Color.Green;
        }
    }
You may need to recursively do the same thing for container controls on the main form.
 
Share this answer
 
Comments
Arasappan 11-Aug-15 14:54pm    
What is this logic griff... he doesn't gave no status..ur not yet clear with newparkinglot().. there is no status..i cant get u griff...
Member 11232188 11-Aug-15 14:56pm    
protected void btnUpadte_Click(object sender, EventArgs e)
{

A1.BackColor = GetColor(A1);
A2.BackColor = GetColor(A2);
.....
.....
A100.BACKCOLOR = GetColor(A100);
}

private Color GetColor(Label label1)
{
ParkingLot p = new ParkingLot();
int x = p.GetStatusLot(label1.ID);
if (x == 1)
{
return Color.Red;
}
else if (x == 2)
return Color.Green;
else if (x == 3)
return Color.Orange;
else
return Color.White;
}
Member 11232188 11-Aug-15 14:57pm    
How can I reduce the code the first function "btnUpadte_Click"
OriginalGriff 12-Aug-15 5:01am    
Just replace the

l.ForeColor = Color.Red;
l.BackColor = Color.Green;

in my code with

l.BackColor - GetColor(l);

Done!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900