用C#计算Button的颜色

Oba*_*ani 0 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++;
}
Run Code Online (Sandbox Code Playgroud)

Son*_*nül 5

我认为正确的语法是;

foreach(Button btn in Tombol)
{
    if(btn.BackColor == Color.Red)
       counterbutton++;
}
Run Code Online (Sandbox Code Playgroud)