本文介绍了如何计算带有红色背景的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨朋友们,
我的Winforms应用程序中有10个标签,c#,背面颜色为红色。
有没有人可以帮忙把它写成输出
红色标签的数量是:10。
谢谢!
我的尝试:
我尝试使用计数器,但没有结果。
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();
推荐答案
int count = 0;
foreach (Control c in this.Controls)
{
if (c.GetType() == typeof(Label))
if (((Label)c).BackColor == Color.Red)
count++;
}
注意:如果标签存在于任何容器控件中,那么您还必须迭代容器,如 []
这篇关于如何计算带有红色背景的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!