平时我们设计互斥选项时,一般采用将几个RadioButton放在一个GroupBox中的方法,但特殊情况下我们需要使用互斥的CheckedListBox控件,这时使用下面的代码即可:
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (this.checkedListBox1.CheckedItems.Count > 0)
{
for (int i = 0; i < this.checkedListBox1.Items.Count; i++)
{
if (i != e.Index)
{
this.checkedListBox1.SetItemCheckState(i, System.Windows.Forms.CheckState.Unchecked);
}
}
}
}
本文介绍了一种在C#中实现CheckedListBox控件互斥选择的方法。通过监听ItemCheck事件并遍历所有项来确保每次只能选中一项,从而达到类似RadioButton的功能。
3448

被折叠的 条评论
为什么被折叠?



