用C#计算Button的颜色

时间:2016-01-20 13:35:06

标签: c# button

我的程序有问题。我有3个按钮,默认颜色为白色。当我的按钮背面颜色变为红色时,我的程序将计算多少个按钮为红色。我有一个想法使用foreach但它不起作用

Button[] Tombol = new Button[]{B1, B2, B3}
int counterbutton = 0;

foreach (Button Tombol2.BackColor = Color.Red in Tombol) //I have problem here. I don't know how to solve
{
  counterbutton++;
}

5 个答案:

答案 0 :(得分:5)

我认为正确的语法是;

foreach(Button btn in Tombol)
{
    if(btn.BackColor == Color.Red)
       counterbutton++;
}

答案 1 :(得分:1)

foreach(Button b in Tombol)
{
    if(b.BackColor == Color.Red)
        counterbutton++;
}

答案 2 :(得分:1)

linq的例子:

var counter = Tombol.Count(b=>b.BackColor == Color.Red)

答案 3 :(得分:0)

{{1}}

答案 4 :(得分:0)

试试这个

var context = new MyContext();
var child = context.Children.Include(c => c.Parent).First();
var parentId = child.Parent.Id;