我使用C#作为Winforms应用程序,我似乎无法更改回默认/未选择的'我的3组单选按钮上的颜色。我知道有一种方法,无需单独按下每个按钮。 foreach
中用于清除已检查状态的部分正常工作。这是我尝试过的。
public void ClearForm()
{
foreach (RadioButton radio1 in carbonationGroupBox.Controls.OfType<RadioButton>().ToList())
if (radio1.Checked == true)
{
radio1.ForeColor = Color.Black;
radio1.Checked = false;
}
foreach (RadioButton radio2 in strengthGroupBox.Controls.OfType<RadioButton>().ToList())
if (radio2.Checked == true)
{
radio2.ForeColor = Color.Black;
radio2.Checked = false;
}
foreach (RadioButton radio3 in sweetnessGroupBox.Controls.OfType<RadioButton>().ToList())
if (radio3.Checked == true)
{
radio3.ForeColor = Color.Black;
radio3.Checked = false;
}
这里是来自其中一个组框的三个单选按钮的代码,这些按钮会改变颜色开始。
private void topStrengthHydromel_CheckedChanged(object sender, EventArgs e)
{
topStrengthStandard.ForeColor = Color.Black;
topStrengthSack.ForeColor = Color.Black;
topStrengthHydromel.ForeColor = Color.Lime;
strengthRadionButton = "Hydromel";
}
private void topStrengthStandard_CheckedChanged(object sender, EventArgs e)
{
topStrengthHydromel.ForeColor = Color.Black;
topStrengthSack.ForeColor = Color.Black;
topStrengthStandard.ForeColor = Color.Lime;
strengthRadionButton = "Standard";
}
private void topStrengthSack_CheckedChanged(object sender, EventArgs e)
{
topStrengthHydromel.ForeColor = Color.Black;
topStrengthStandard.ForeColor = Color.Black;
topStrengthSack.ForeColor = Color.Lime;
strengthRadionButton = "Sack";
}
答案 0 :(得分:1)
我发现的问题是foreach
区域内的事件顺序。
此订单:
if (radio1.Checked == true)
{
radio1.ForeColor = Color.Black;
radio1.Checked = false;
}
触发_CheckChanged事件并将颜色重新添加到Lime。
private void topStrengthHydromel_CheckedChanged(object sender, EventArgs e)
{
topStrengthStandard.ForeColor = Color.Black;
topStrengthSack.ForeColor = Color.Black;
topStrengthHydromel.ForeColor = Color.Lime;
strengthRadioButton = "Hydromel";
}
切换顺序,以便radio1.Checked = false
在radio1.ForeColor = Color.Black;
完成之前发生了{{1}}!