[英]C# RadioButton not Working Properly
private void SelectingNumberOfStorey()
{
RadioButton_1Storey.CheckedChanged += (sender, args) =>
{
NumberOfStorey = 1;
HidingFirstQuestions();
};
RadioButton_2Storey.CheckedChanged += (sender, args) =>
{
NumberOfStorey = 2;
HidingFirstQuestions();
};
if (Runner == "Must Run")
{
AddCheckBox();
AddGrid();
}
}
private void HidingFirstQuestions()
{
DialogResult dialogResult = MessageBox.Show("You Select " + NumberOfStorey + " Storey!", "Selection", MessageBoxButtons.OKCancel);
if (dialogResult == DialogResult.OK)
{
Runner = "Must Run";
}
else if (dialogResult == DialogResult.Cancel)
{
//do nothing
}
}
If I will clicked the RadioButton_1Storey and clicked cancel in messagebox for the first time, it works just fine. 如果我将第一次单击RadioButton_1Storey并在消息框中单击“取消”,它就可以正常工作。 But when I clicked RadioButton_2Storey then cancel, the previous radiobutton "RadioButton_1Storey" will execute 1 more time before the RadioButton_2Storey start executing. 但是,当我单击RadioButton_2Storey然后取消时,上一个单选按钮“ RadioButton_1Storey”将在RadioButton_2Storey开始执行之前执行另外1次。 And Vice Versa 和副Versa
when you click RadioButton_2Storey, RadioButton_1Storey.Checked
becomes false
, and raise CheckedChanged
event and HidingFirstQuestions
runs 当您单击RadioButton_2Storey时, RadioButton_1Storey.Checked
变为false
,并引发CheckedChanged
事件并运行HidingFirstQuestions
modify event handlers like this 像这样修改事件处理程序
RadioButton_1Storey.CheckedChanged += (sender, args) =>
{
if (RadioButton_1Storey.Checked)
{
NumberOfStorey = 1;
HidingFirstQuestions();
}
};
RadioButton_2Storey.CheckedChanged += (sender, args) =>
{
if (RadioButton_2Storey.Checked)
{
NumberOfStorey = 2;
HidingFirstQuestions();
}
};
just to be sure that your code isn't called twice, I'd change it to: 只是为了确保您的代码不会被调用两次,我将其更改为:
private void SelectingNumberOfStorey()
{
RadioButton_1Storey.CheckedChanged -= RadioButton_1StoreyCheckedChanged;
RadioButton_1Storey.CheckedChanged += RadioButton_1StoreyCheckedChanged;
RadioButton_2Storey.CheckedChanged -= RadioButton_2StoreyCheckedChanged;
RadioButton_2Storey.CheckedChanged += RadioButton_2StoreyCheckedChanged;
...
}
private void RadioButton_1StoreyCheckedChanged(object sender, EventArgs args)
{
...
}
private void RadioButton_2StoreyCheckedChanged(object sender, EventArgs args)
{
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.