Click here to Skip to main content
15,905,967 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi friends,

I have ten labels in my Winforms app in c#, they have back color "red".

Does any one can just help to write this as output

The number of red labels are: 10.

Thank You!

What I have tried:

I try with counter, but no result.

Label lb;
            int count = 1; ;
            if (lb.BackColor == Color.Red)
{ 
count++;
}

                label6.Text = count.ToString();
Posted
Updated 1-Feb-17 18:51pm
v2
Comments
CHill60 1-Feb-17 8:56am    
What CODE have you tried? Share it.
Is this Winforms or WPF?
[no name] 1-Feb-17 8:59am    
"Does any one can just help to write this as output", sure thing.
Console.WriteLine("The number of red labels are: 10.");
h5h5 1-Feb-17 9:10am    
In fact counting them
not just writting, the code to count labels and then as out put, to be 10

This is how you probably want to do it...

Initialise a integer counter.

Use a foreach loop (Ref:foreach, in (C# Reference)[^]) to step through all of the controls on your form using the Controls collection of the form (Ref: How to: Access Controls by using the Controls Collection[^] )

Test the type of each control using control.GetType() (Ref: Object.GetType Method (System)[^] ) and compare it to the typeof Label (Ref:typeof (C# Reference)[^])

If they match types then you can check to see if BackColor is Color.Red ... Hint, you might have to cast the control to an explicit Label in order to access the BackColor property (Ref: Casting and Type Conversions (C# Programming Guide)[^] )

You can use MessageBox.Show() to display the results (Ref: MessageBox.Show Method (System.Windows.Forms)[^] )
 
Share this answer
 
Comments
h5h5 1-Feb-17 9:26am    
ehI am still taking steps in programing.
I appreciate this help I will try, but I dont think I can do it..
If you just can write some codes.
By the way
thank you
CHill60 1-Feb-17 10:13am    
If I just write the code for you then you will never learn and neither you nor I will benefit.
Give it a go, it's not as hard as it seems - especially if you have a look at the "How to:" link I gave you ... it comes with code! You will learn much more by trying and failing than by just copying (believe me ... I've been there). If you get stuck then come back with the code you have go to so far... take small steps and use the debugger step by step so you can see exactly what is going on.
Good Luck
[no name] 1-Feb-17 10:37am    
Sorry but if after FIVE YEARS of learning C# and you can't do something as simple as creating a for loop and finding the labels you are looking for, it's time to find another hobby.
WinForm[^] inherits from Control and Control contains a collection of Controls[^] ;)
So, you can iterate through the collection of controls. But, i need to warn you! Some of controls can contain another controls...
 
Share this answer
 
try this
C#
int count = 0;
            foreach (Control c in this.Controls)
            {
                if (c.GetType() == typeof(Label))
                    if (((Label)c).BackColor == Color.Red)
                        count++;
            }

Note: if the labels are present in any container control, then you will have to iterate the container also, like this[^]
 
Share this answer
 
Comments
h5h5 2-Feb-17 8:42am    
HI Karthik, I used it, there is no error, but when I want to show then number of labels with red colore there is 0 labels in fact I have 10 red labels.
I used that code and this one:
label.text= "Red Labeles:" + count;
and the result is
Red Labels: 0
h5h5 3-Feb-17 3:44am    
I try this, all labels are in a Form not in container.
int count = 0;
foreach (Control c in this.Controls)
{
if (c.GetType() == typeof(Label))
if (((Label)c).BackColor == Color.Red)
count++;
}
label16.Text = "Label: " + count;

I have again the same problem, there are not counting the result is again Label: 0
Karthik_Mahalingam 2-Feb-17 8:44am    
does the label controls are placed in the form only or inside a container?
h5h5 2-Feb-17 9:30am    
Label are inside the group box, in the load worm they are hidden, when I push button result, labels are showed, there many labels but I want just labels with red color to count and as output to be: Red Labels are: 10
Karthik_Mahalingam 2-Feb-17 10:58am    
Use groupboxname.controls in place of "this.controls"

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