日期:2014-05-18 浏览次数:21303 次
private void cmbDevity_SelectedIndexChanged(object sender, EventArgs e)//根据combobox选择 { foreach (Control c in Controls) //清空颜色 { if (c is Button) { c.BackColor = Color.Transparent; } } if (this.cmbDevity.Text.Equals("奇数")) { foreach (Control c in Controls) //遍历控件,判断 { if (c is Button && int.Parse(c.Text) % 2 != 0) { c.BackColor = Color.Yellow; } } } else { foreach (Control c in Controls) { if (c is Button && int.Parse(c.Text) % 2 == 0) { c.BackColor = Color.Yellow; } } } }
------解决方案--------------------
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
int index = comboBox1.SelectedIndex;
switch (index)
{
//全选,所有按钮紫色
case 0:
button1.BackColor = Color.Fuchsia;
button2.BackColor = Color.Fuchsia;
button3.BackColor = Color.Fuchsia;
button4.BackColor = Color.Fuchsia;
button5.BackColor = Color.Fuchsia;
button6.BackColor = Color.Fuchsia;
button7.BackColor = Color.Fuchsia;
button8.BackColor = Color.Fuchsia;
button9.BackColor = Color.Fuchsia;
button10.BackColor = Color.Fuchsia;
break;//不要跳到下个分支
case 1://奇数
button1.BackColor = Color.Transparent;
button2.BackColor = Color.Fuchsia;
button3.BackColor = Color.Transparent;
button4.BackColor = Color.Fuchsia;
button5.BackColor = Color.Transparent;
button6.BackColor = Color.Fuchsia;
button7.BackColor = Color.Transparent;
button8.BackColor = Color.Fuchsia;
button9.BackColor = Color.Transparent;
button10.BackColor = Color.Fuchsia;
break;//不要跳到下个分支
case 2://偶数
button1.BackColor = Color.Fuchsia;
button2.BackColor = Color.Transparent;
button3.BackColor = Color.Fuchsia;
button4.BackColor = Color.Transparent;
button5.BackColor = Color.Fuchsia;
button6.BackColor = Color.Transparent;
button7.BackColor = Color.Fuchsia;
button8.BackColor = Color.Transparent;
button9.BackColor = Color.Fuchsia;
button10.BackColor = Color.Transparent;
break;//不要跳到下个分支
case 3://大数
button1.BackColor = Color.Transparent;
button2.BackColor = Color.Transparent;
button3.BackColor = Color.Transparent;
button4.BackColor = Color.Transparent;
button5.BackColor = Color.Transparent;
button6.BackColor = Color.Fuchsia;
button7.BackColor = Color.Fuchsia;