Results 1 to 5 of 5

Thread: Removing All Checked Items

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2009
    Posts
    11

    Removing All Checked Items

    Hello, I have a slight problem. I'm trying to remove all the checked items in my CheckedListBox.
    I started by doing CheckedListBox1.Items.Remove(CheckedListbox1.CheckedItems). That seemed like it would work, but it didn't. I soon discovered that CheckedListBox1.Items.Remove only works on the the strings of the individual items.

    Is there a way to accomplish what I am trying to do?

    Thanks for the help.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: Removing All Checked Items

    try this:

    vb Code:
    1. For x As Integer = CheckedListBox1.Items.Count - 1 To 0 Step -1
    2.     If CheckedListBox1.CheckedIndices.Contains(x) Then CheckedListBox1.Items.RemoveAt(x)
    3. Next

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2009
    Posts
    11

    Re: Removing All Checked Items

    It worked very nicely, thank you.

    Quick question: I don't quite understand the "To 0 Step -1" part of the code, I would think that the Items.Count part would return a positive number.

    Thanks.

  4. #4
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Removing All Checked Items

    Instead of incrementing the counter (x) by one he is decreasing it by 1. So if you have 10 items in your CheckedListBox, it basically translates into: Starting from 9 go to 0 decreasing by 1 each time.

  5. #5

    Thread Starter
    New Member
    Join Date
    Jul 2009
    Posts
    11

    Re: Removing All Checked Items

    Oh, now I see, that makes sense.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width