1.先创建一个panle控件,将所需改变颜色的button放置在panle控件中,使用下列代码即可
private void button1_Click(object sender, EventArgs e)
{
test(sender);
}
private void test(object s)
{
foreach (System.Windows.Forms.Control item in panel1.Controls)
{
if (item is System.Windows.Forms.Button && item.Text == ((System.Windows.Forms.Button)s).Text)
{
item.BackColor = Color.Red;
}
else
{
item.BackColor = Color.Transparent;
}
}
}