听起来确实像一个单选按钮。但是,如果您坚持使用复选框,请尝试以下操作:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
checkBox2.Checked = !checkBox1.Checked;
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
checkBox1.Checked = !checkBox2.Checked;
}
编辑:如果您有两个以上的复选框(甚至有两个),您可以创建一个循环遍历所有复选框并更改它们的过程,例如(伪代码):
private void CheckAllCheckboxes(checkbox ACheckbox) //ACheckbox is the checkbox just clicked
{
bool JustChecked = ACheckbox.checked; //did you enable or disable ACheckbox?
for (int i = 0; i <= AllCheckBoxes.count; i++)
{
if (AllCheckboxes[i] != ACheckbox) //every checkbox except the one just checked...
{
AllCheckBoxes[i].checked = !JustChecked //set the opposite
}
}
}