110,549
社区成员
发帖
与我相关
我的任务
分享
//用一个List保存所有的CheckListBox
private List<CheckedListBox> clbList;
clbList = new List<CheckedListBox>();
//将相关的CheckedListBox添加到List中
clbList.AddRange(new CheckedListBox[] { checkedListBox1, checkedListBox2, checkedListBox3, checkedListBox4 });
//注册每个CheckedListBox的ItemCheck事件
foreach (CheckedListBox clb in clbList)
{
clb.ItemCheck += this.OnItemCheck;
}
private void OnItemCheck(object sender, ItemCheckEventArgs e)
{
CheckedListBox clb = sender as CheckedListBox;
//找到当前的CheckListBox在List中的位置
int index = clbList.IndexOf(clb);
//从最后一个CheckedListBox开始设置check状态
for (int i = clbList.Count - 1; i > index; i--)
{
clb = clbList[i];
//先注销事件是为了防止OnItemCheck被重复调用
clb.ItemCheck -= this.OnItemCheck;
//设置状态
for (int j = 0; j < clb.Items.Count; j++)
{
clb.SetItemChecked(j, true);
}
//重新注册ItemCheck时间
clb.ItemCheck += this.OnItemCheck;
}
}