How to clear selected items from multiple checkedlistboxes
-
Hi, I have a WindowsForm, which has a groupBox and has multiple CheckedListBox Controls. I want to clear all selected check items dynamically, instead of doing it individually. I want to get all the controls from the groupBox first, which also has other controls, then get the CheckedListBox controls and clearing it, if selected. I wrote this code, but it is not working, its not coming out of the loop. Any help, will be really appreciated. foreach(Control ctrl in this.groupBox1.Controls) { if(ctrl is CheckedListBox) { CheckedListBox cb=(CheckedListBox)ctrl; for(int i=0; i<=(cb.CheckedItems.Count-1); i++) { cb.ClearSelected(); } } } abhi
-
Hi, I have a WindowsForm, which has a groupBox and has multiple CheckedListBox Controls. I want to clear all selected check items dynamically, instead of doing it individually. I want to get all the controls from the groupBox first, which also has other controls, then get the CheckedListBox controls and clearing it, if selected. I wrote this code, but it is not working, its not coming out of the loop. Any help, will be really appreciated. foreach(Control ctrl in this.groupBox1.Controls) { if(ctrl is CheckedListBox) { CheckedListBox cb=(CheckedListBox)ctrl; for(int i=0; i<=(cb.CheckedItems.Count-1); i++) { cb.ClearSelected(); } } } abhi
Hmm, that is rather strange. Here is a quick work around.
private void ClearSelected() { CheckedListBox clb = null; foreach(Control c in grp.Controls) { if(c is CheckedListBox) { clb = c as CheckedListBox; if(clb != null) { foreach(int index in clb.CheckedIndices) clb.SetItemChecked(index, false); } } } }
- Nick Parker
My Blog | My Articles -
Hi, I have a WindowsForm, which has a groupBox and has multiple CheckedListBox Controls. I want to clear all selected check items dynamically, instead of doing it individually. I want to get all the controls from the groupBox first, which also has other controls, then get the CheckedListBox controls and clearing it, if selected. I wrote this code, but it is not working, its not coming out of the loop. Any help, will be really appreciated. foreach(Control ctrl in this.groupBox1.Controls) { if(ctrl is CheckedListBox) { CheckedListBox cb=(CheckedListBox)ctrl; for(int i=0; i<=(cb.CheckedItems.Count-1); i++) { cb.ClearSelected(); } } } abhi
Consider that you might have nested controls, that a
CheckedListBox
may not be a direct child of aGroupBox
. Consider a generic recursive loop like so:void ClearSelected(ControlCollection controls)
{
if (controls == null || controls.Count == 0) return; // Quick
foreach (Control control in controls)
{
if (control is CheckedListBox) ((CheckedListBox)control).ClearSelected();
ClearSelected(control.Controls);
}
}This will encounter any
CheckedListBox
at any depth in the parent/child hierarchy of controls. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles]