-1
我有一个checkedListBox
和一个selectAll checkBox
。如何将selectAll与checkedListBox一起使用..?
我想要的是,如果checkedListBox中的元素没有被选中,那么selectAll checkBox
会被取消选中(如果选中)。
我正在使用Winforms
并使用C#。
我有一个checkedListBox
和一个selectAll checkBox
。如何将selectAll与checkedListBox一起使用..?
我想要的是,如果checkedListBox中的元素没有被选中,那么selectAll checkBox
会被取消选中(如果选中)。
我正在使用Winforms
并使用C#。
您需要使用两个事件,选中复选框改变(检查所有项目)和CheckedListBox ItemChecked更新的复选框:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (!checkBox1.Checked) return;
for (int i = 1; i < checkedListBox1.Items.Count; i++)
checkedListBox1.SetItemChecked(i, checkBox1.Checked);
}
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
for (int i = 1; i < checkedListBox1.Items.Count; i++)
{
if(checkedListBox1.GetItemCheckState(i).Equals(CheckState.Unchecked){
checkBox1.Checked=false;
return;
}
}
checkBox1.Checked=true;
}