XTab's Blog

Ged Mead's Blog at vbCity

This blog hosted by:
http://blogs.vbcity.com      
  Home :: Syndication  :: Login

AprMay 2007Jun
SMTWTFS
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

Archives

Topics

Ramblings

VB.NET

Wednesday, May 16, 2007 #

   This blog item was originally going to be about ten lines long and all I intended to show was how to programatically uncheck all items in a CheckedListBox in VB 2005.  But, you know how it is, you think "Hmm, what about if ...?"  or "What if I ... ?" and before you know it, you've got a mini-article.   So, before I forget why I started writing this item, here is the code needed to uncheck all items in a CheckedListBox:

Programatically Uncheck All Items

Code Copy
        For i As Integer = 0 To CheckedListBox1.Items.Count - 1
            CheckedListBox1.SetItemChecked(i, False)
        Next

    It's one of those infuriatingly simple things (some might say "obvious") but it's only obvious if you have already tracked down the answer and know about the SetItemChecked method.  It's a bit like Quiz questions - the only hard ones are those you don't know the answer to. 

   Then I remembered that someone once asked about how to delete items that have been checked, so I thought I'd include that too.

Programatically Delete Checked Items   

Code Copy
With CheckedListBox1
   If .CheckedItems.Count > 0 Then
       For x As Integer = .CheckedItems.Count - 1 To 0 Step -1
             .Items.Remove(.CheckedItems(x))
       Next
   End If
End
With

    You can probably see how it works.  Again, it's easy if you know that the CheckedListBox has a CheckedItems property which is a collection of those items that are currently checkmarked.   By iterating through that collection you can identify each checked item in turn and do something with it or to it.   In this case we removed it.

   You may also have noticed that the Loop starts at the end of the collection (Count - 1) and steps backwards.  This is standard practice with collections.  If you don't approach it this way you will get an error; the compiler can't handle the dynamically changing number of elements, gets confused and throws an Exception at you.   Working from the end backwards neatly avoids this problem.

Comparing Lists And Checking Matches

   Setting the SetItemChecked property to False, as we saw originally, unchecks items.   So obviously setting the value to True will check them.    One situation where you might want to programatically check one or more CheckedListBox items  may be as a result of a comparison. 

  For instance, you might take some user input in a TextBox and if there is a matching item in the CheckedListBox then you flag this up by checking that item. 

Code Copy
        With CheckedListBox1
            If .Items.Count > 0 Then
                .SetItemChecked(.FindStringExact(TextBox1.Text), True)
            End If
        End With

   Note that I first test that the CheckedListBox contains at least one item.  Failure to account for this will also result in an Exception being thrown.  An empty TextBox won't cause a comparable problem.  (In fact if for some reason you do have a list item that contains no text then it will be found and it's checkbox checked in this situation). 

   Interestingly, although the method is called "FindStringExact" it doesn't seem to be case sensitive.  There may of course be a further tweak available for that kind of finer granularity but I haven't discovered it yet.   Also the above version only finds the first occurrence of a match.

   To find and check all matches found in the CheckedListBox you can use this code instead:

Code Copy
        With CheckedListBox1
            If .Items.Count > 0 Then
                For i As Integer = 0 To .Items.Count - 1
                    If Trim(.Items(i).ToString) = Trim(TextBox1.Text) Then
                        .SetItemChecked(i, True)
                    End If
                Next
            End If
        End With

     It might not be quite as concise, but it does the job.

    More realistically, you might pull items from a database or a text file and check any matches to be found in the listbox.   And that's something we'll look at in Part 2, along with various other approaches to saving and reading checkedlistbox items (and their check states) to/from files.

  

posted @ 11:02 AM | Feedback (3)