Checkedlistbox.CheckedItems Issue.


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Checkedlistbox.CheckedItems Issue.

  1. #1
    Join Date
    Aug 2005
    Posts
    7

    Checkedlistbox.CheckedItems Issue.

    I am working with a checkedlistbox and I am having a small issue. Here is the code that I am using.

    Private Sub CListBox1_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles CListBox1.ItemCheck
    Dim itemchecked As Object

    For Each itemchecked In CListBox1.CheckedItems
    MsgBox(itemchecked)
    Next

    End Sub

    Now the problem that I am having is when I check an item in the listbox it is suppost to bring up a msgbox displaying each item that is checked, however; that isn't what happens. Instead I get a msgbox that displays all the times thats checked except for the one that I just checked.

    For example:
    I check Item#1 - Nothing happens
    Then I check Item #2 - Msgbox comes up with the name of Item#1 and then nothing else.
    Item# 3 - I get a msgbox for 1 & 2 but not 3 and so on.

    What am I doing wrong?

  2. #2
    Join Date
    Aug 2005
    Posts
    7
    I might also note that if I change:
    msgbox(itemchecked)

    to

    msgbox(clistbox1.checkeditems.count)

    I get a msgbox displaying the number of items that is checked, however; it is also 1 short of what it should be.

  3. #3
    Join Date
    Aug 2005
    Posts
    6
    I think this event fires when you click on the item but before it is considered to be checked. This would allow some pre processing/validation before an item is selected.

  4. #4
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    As noted in the documentation, the ItemCheck event "occurs when the checked state of an item changes." In other words, it's intended to provide information about the checked state of a single item, not the entire list.

    Try this:
    Code:
    Private Sub CListBox1_ItemCheck(ByVal sender As Object, _ 
        ByVal e As System.Windows.Forms.ItemCheckEventArgs) _ 
        Handles CListBox1.ItemCheck
    
        ' Display msgbox for previous checked items
        For Each itemchecked As Object In CListBox1.CheckedItems
            MsgBox(itemchecked)
        Next
    
        ' If this event is fired because item was checked,
        If e.NewValue <> CheckState.Unchecked Then
            ' Display msgbox for newly-checked item
            MsgBox(CListBox1.Items(e.Index))
        End If
    
    End Sub
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

Similar Threads

  1. Deadlock issue in SQL Server
    By manumishra in forum Database
    Replies: 5
    Last Post: 06-09-2008, 05:28 PM
  2. Issue with VS.NET Find
    By Beginner in forum .NET
    Replies: 0
    Last Post: 01-27-2002, 06:35 PM
  3. Replies: 0
    Last Post: 09-19-2001, 07:48 AM
  4. bizzare issue w/ cells - not in UP sim
    By Michael in forum Mobile
    Replies: 1
    Last Post: 07-11-2001, 06:18 PM
  5. Application Server Newsletter Issue
    By John Lockwood in forum Web
    Replies: 1
    Last Post: 04-06-2000, 05:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center