Click here to Skip to main content
15,890,845 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have multiple label how to change forecolor of labels using multiple for loop in windows form application
Posted
Comments
Aydin Homay 17-Jun-13 8:19am    
What is your meaning of multiple label? are you have a list<label> or same some thing?
Member 10054218 17-Jun-13 8:31am    
i make a one application in that i have multiple label in this label i declare time and i compare current time and my label time.i put on nested for loop

Something like this should do the trick (assumes that the code is part of a method on the form):
XML
Color newColor = Color.Red;
foreach (var lbl in Controls.OfType<Label>())
    lbl.ForeColor = newColor;
 
Share this answer
 
Comments
johannesnestler 17-Jun-13 8:51am    
cool code - If OP's Labels are not on one Form he could just "collect" them together to a "global" List of type Label(-References).
JayantaChatterjee 17-Jun-13 10:32am    
My vote 5, for new technique..... :-)
Call this code on your buttonclick event
C#
foreach (Control control in this.Controls)
           {
               if (control is Label)
               {
                   (control as Label).ForeColor = Color.Red;
               }
           }
 
Share this answer
 
Comments
johannesnestler 17-Jun-13 8:50am    
Chris Ross' solution using modern .NET style looks better, but yours is also useful if OP uses .NET < 3.
Chris Ross 2 17-Jun-13 9:15am    
Very good point!

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