C# 计数按钮';C中的s颜色#
我的程序有问题。我有3个按钮,默认颜色是白色。当我的按钮背面颜色变为红色时,我的程序将计算有多少按钮是红色的。我有一个使用foreach的想法,但它不起作用C# 计数按钮';C中的s颜色#,c#,button,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 solv
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++;
}
我认为正确的语法是
foreach(Button btn in Tombol)
{
if(btn.BackColor == Color.Red)
counterbutton++;
}
试试这个
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Button[] Tombol = new Button[]{B1, B2, B3};
int counterbutton = 0;
B1.BackColor = Color.Red;
B3.BackColor = Color.Red;
foreach (Button b in Tombol)
{
if(b.BackColor == Color.Red) //I have problem here. I don't know how to solve
{
counterbutton++;
}
}
}
}
}
linq的一个示例:
var counter = Tombol.Count(b=>b.BackColor == Color.Red)
顺便问一下,
Tombol2
是什么?Tombol2是按钮[]中的所有按钮Tombol:)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Button[] Tombol = new Button[]{B1, B2, B3};
int counterbutton = 0;
B1.BackColor = Color.Red;
B3.BackColor = Color.Red;
foreach (Button b in Tombol)
{
if(b.BackColor == Color.Red) //I have problem here. I don't know how to solve
{
counterbutton++;
}
}
}
}
}
var counter = Tombol.Count(b=>b.BackColor == Color.Red)