Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to clear selected items from multiple checkedlistboxes

How to clear selected items from multiple checkedlistboxes

Scheduled Pinned Locked Moved C#
helptutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    abhishk2001 yahoo com
    wrote on last edited by
    #1

    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

    N H 2 Replies Last reply
    0
    • A abhishk2001 yahoo com

      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

      N Offline
      N Offline
      Nick Parker
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • A abhishk2001 yahoo com

        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

        H Offline
        H Offline
        Heath Stewart
        wrote on last edited by
        #3

        Consider that you might have nested controls, that a CheckedListBox may not be a direct child of a GroupBox. 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]

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups