locked
ListView, ItemCheck and getting count RRS feed

  • Question

  • Hello,

    I have a device app with a ListView control with CheckBoxes set to true.  When the user checks/unchecks an item, I need to perform some steps, then update a control with the count of how many items are checked.  I'm having trouble because the ItemCheck event occurs before the actual value of the checkbox changes, so when I do something like the code below, the count is never correct because I'm trying to update the count before the value actually gets changed.  I tried using the SelectedIndexChange event but that doesn't seem to fire when I check/uncheck the box.  Any suggestions?

    Also, when I add an item to the list with the .Checked value = True, the ItemCheck event fires but e.CurrentValue is {0} false for some reason. 

      ...
    
       With .lstBarrels
    
            If e.CurrentValue = CheckState.Checked Then
    
                UpdateOutputTables("D", .Items(e.Index).Text)
    
            Else
    
                UpdateOutputTables("I", .Items(e.Index).Text)
    
            End If
    
        End With
    
    ...
    
    
    
        Private Function UpdateOutputTables(ByVal InsDelUpd As String, ByVal BarrelID As String) As Boolean
    
    
    
            ...do some stuff
    
    
    
            .txtTotBbls.Text = ListViewCheckedCount(.lstBarrels)
    
    
    
        End Function
    
    
    
        Public Function ListViewCheckedCount(ByVal theList As ListView) As Integer
    
            Dim LstItem As New ListViewItem
    
            Try
    
                ListViewCheckedCount = 0
    
    
    
                With theList
    
                    For Each LstItem In theList.Items
    
                        If LstItem.Checked = True Then
    
                            ListViewCheckedCount = ListViewCheckedCount + 1
    
                        End If
    
                    Next
    
                End With
    
    
    
            Catch ex As Exception
    
                DisplayDeviceErrors(ex, Nothing)
    
            End Try
    
    
    
            LstItem = Nothing
    
    
    
        End Function
    Monday, March 22, 2010 7:00 PM

Answers

  • Hi gcresse,

    Like you said, in ItemCheck event, its checked status is not change. We can check e.NewValue, if it is "Checked", it show its previous status is "Unchecked". So in your ListViewCheckedCount method, we should add 1 or subtract 1.

     

    >>Also, when I add an item to the list with the .Checked value = True, the ItemCheck event fires but e.CurrentValue is {0} false for some reason. 

    Here We also need to check its NewValue, not CurrentValue. If NewValue is checked, it will be checked now.

     

    Best regards,
    Guang-Ming Bian - MSFT


    Please remember to mark the replies as answers if they help and unmark them if they provide no help
    Wednesday, March 24, 2010 6:51 AM

All replies

  • Hi gcresse,

    Like you said, in ItemCheck event, its checked status is not change. We can check e.NewValue, if it is "Checked", it show its previous status is "Unchecked". So in your ListViewCheckedCount method, we should add 1 or subtract 1.

     

    >>Also, when I add an item to the list with the .Checked value = True, the ItemCheck event fires but e.CurrentValue is {0} false for some reason. 

    Here We also need to check its NewValue, not CurrentValue. If NewValue is checked, it will be checked now.

     

    Best regards,
    Guang-Ming Bian - MSFT


    Please remember to mark the replies as answers if they help and unmark them if they provide no help
    Wednesday, March 24, 2010 6:51 AM
  • Thanks, the e.NewValue is what I want to check for in the ItemCheck event instead of e.CurrentValue. 

    While ItemCheck is still firing, I'm trying to get a count of the checked items and since it hasn't "really" been checked yet, I guess there's no way for the ListViewCheckedCount function to return the correct count.  I think I will have to come up with a different way to get the count.

    Thursday, March 25, 2010 9:55 PM
  • Hi gcresse,

    Like you said, the listview checked count is not correct in ItemCheck method, we have to add or subtract 1 manually.

     

    Best regards,
    Guang-Ming Bian - MSFT


    Please remember to mark the replies as answers if they help and unmark them if they provide no help
    Monday, March 29, 2010 2:57 AM