[英]C# UserControl Not and Yes with Text
The Default is No Checkbox默认为无复选框
When I run the program and Click the Yes Checkbox the program overflowed当我运行程序并单击是复选框时程序溢出
private void checkEdit1_Click(object sender, EventArgs e)
{
checkEdit2.Checked = false;
textEdit1.Enabled = true;
answered = true;
optional = textEdit1.Text;
if (!checkEdit1.Checked)
{
checkEdit1.Checked = true;
checkEdit2.Checked = false;
textEdit1.Enabled = true;
optional = textEdit1.Text;
}
}
private void checkEdit2_Click(object sender, EventArgs e)
{
checkEdit1.Checked = false;
textEdit1.Enabled = false;
answered = false;
if (!checkEdit2.Checked)
{
checkEdit2.Checked = true;
checkEdit1.Checked = false;
textEdit1.Enabled = false;
answered = false;
}
}
What you think is the error ?你认为是什么错误?
Instead of the Click
event you should use the CheckedChanged
event in this way:您应该以这种方式使用
CheckedChanged
事件,而不是Click
事件:
checkEdit1.CheckedChenged += new EventHandler(checkEdit1_CheckedChanged);
checkEdit2.CheckedChenged += new EventHandler(checkEdit2_CheckedChanged);
private void checkEdit1_CheckedChanged(object sender, EventArgs e)
{
if(checkEdit1.Checked == checkEdit2.Checked)
checkEdit2.Checked = !checkEdit.Checked;
}
private void checkEdit2_CheckedChanged(object sender, EventArgs e)
{
if(checkEdit1.Checked == checkEdit2.Checked)
checkEdit2.Checked = !checkEdit.Checked;
}
But the best way in this case is to use a group of radio buttons.但在这种情况下最好的方法是使用一组单选按钮。
Assuming that those methods are wired up to checkEdit1
and checkEdit2
I would advise that you don't make a change to checkEdit1
in checkEdit1_Click
as it has already changed - only modify the state of the alternate.假设这些方法已连接到
checkEdit1
和checkEdit2
我建议您不要在checkEdit1_Click
对checkEdit1
进行更改,因为它已经更改了 - 仅修改备用状态。
However, when you modify the state of the other, unless you're careful, you're going to get called back.然而,当你修改对方的状态时,除非你小心,否则你会被召回。 Eventually the computer gives up -- the overflow!
最终计算机放弃了——溢出!
As mentioned in a comment by @Cyborgx37, radio buttons are the better UX choice here!正如@Cyborgx37 在评论中提到的,单选按钮是这里更好的 UX 选择!
A possible solution, bind a single method to the OnClick to BOTH checkboxes:一个可能的解决方案,将单个方法绑定到 OnClick to BOTH 复选框:
private bool internallyUpdating = false;
private void CheckboxClick(object sender, EventArgs e)
{
if ( !internallyUpdating )
{
// Prevent subsequent changes
internallyUpdating = true;
// Exchange 'checked' state
if ( sender == checkEdit1 )
{
checkEdit2.Checked = !checkEdit2.Checked;
}
else // if (sender == checkEdit2)
{
checkEdit1.Checked = !checkEdit1.Checked;
}
// other logic here..
// restore 'on change' functionality.
internallyUpdating = false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.