Results 1 to 18 of 18

Thread: Listview checkboxes count

Hybrid View

  1. #1
    Hyperactive Member
    Join Date
    Sep 10
    Posts
    309

    Listview checkboxes count

    Hi guys,

    Can you please help me with the listview. I am working on my listview as I want to count how many checkboxes that I have ticked and unticked on my listview. Please can you tell me how I could do this?

    If you do know how I could do this in simple way, I would be very grateful if you could tell me how I could do this.


    Thanks,
    PillKilla

  2. #2
    Fanatic Member ident's Avatar
    Join Date
    Mar 09
    Location
    Cambridge
    Posts
    1,021

    Re: Listview checkboxes count

    Code:
    Dim cItems As ListView.CheckedListViewItemCollection = Me.ListView1.CheckedItems
    MsgBox(cItems.Count)

  3. #3
    Frenzied Member
    Join Date
    Sep 06
    Posts
    1,258

    Re: Listview checkboxes count

    Try this
    vb Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         For j As Integer = 0 To ListView1.Items.Count - 1
    3.             If ListView1.Items.Item(j).Checked Then
    4.                 Debug.Print("Item number " & j + 1 & " is checked.")
    5.             End If
    6.         Next
    7.     End Sub

  4. #4
    Frenzied Member MattP's Avatar
    Join Date
    Dec 08
    Location
    WY
    Posts
    1,189

    Re: Listview checkboxes count

    vb.net Code:
    1. Dim query = TestListView.Items.Cast(Of ListViewItem)()
    2.         Dim checked = query.Where(Function(item) item.Selected = True).Count()
    3.         Dim unchecked = query.Where(Function(item) item.Selected = False).Count()
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  5. #5
    Hyperactive Member
    Join Date
    Sep 10
    Posts
    309

    Re: Listview checkboxes count

    Thanks guys, but none of them have help me to solve the problem.

    I want the messagebox to display when I have click on the checkboxes.

    I have one checkbox ticked and when I click on the other checkbox to get tick, I get the messagebox display twice which it said that I have click on item 1 and item 2 which i have only click on item 2. The item 1 have already ticked itself when I ran the form.

    Here's the code I have used:

    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                For j As Integer = 0 To ListView1.Items.Count - 1
                    If ListView1.Items.Item(j).Checked Then
                        Debug.Print("Item number " & j + 1")
                    End If
                Next
            End Sub
    I find that the code are not doing it correctly. I only want the messagebox to display in what checkbox i have tick and untick while the other checkboxes are already ticked and unticked.

    Any idea how i can get the messagebox to display as when I click on the checkboxes depend on what checkboxes i have click on?

  6. #6
    Frenzied Member
    Join Date
    Sep 06
    Posts
    1,258

    Re: Listview checkboxes count

    Put your code inside ItemChecked event

    vb Code:
    1. Private Sub ListView1_ItemChecked(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckedEventArgs) Handles ListView1.ItemChecked
    2.         If e.Item.Checked Then
    3.             MsgBox(e.Item.Text & " has been checked")
    4.         Else
    5.             MsgBox(e.Item.Text & " has been unchecked")
    6.         End If
    7.     End Sub

  7. #7
    Hyperactive Member
    Join Date
    Sep 10
    Posts
    309

    Re: Listview checkboxes count

    Thanks 4x2y, but I only want to use it in the button1 click event.

    Any idea how I can get the messagebox to display when I click on the button after I have click on the checkboxes in the listview?

  8. #8
    Frenzied Member
    Join Date
    Sep 06
    Posts
    1,258

    Re: Listview checkboxes count

    Save the checked item in a module level variable and use it in button click event, like this
    vb Code:
    1. Public Class Form1
    2.  
    3.     Private mlviLatestCheckedItem As ListViewItem
    4.  
    5.     Private Sub ListView1_ItemChecked(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckedEventArgs) Handles ListView1.ItemChecked
    6.         mlviLatestCheckedItem = e.Item
    7.     End Sub
    8.  
    9.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    10.         If mlviLatestCheckedItem.Checked Then
    11.             MsgBox(mlviLatestCheckedItem.Text & " has been checked")
    12.         Else
    13.             MsgBox(mlviLatestCheckedItem.Text & " has been unchecked")
    14.         End If
    15.     End Sub
    16.  
    17. End Class

  9. #9
    Hyperactive Member
    Join Date
    Sep 10
    Posts
    309

    Re: Listview checkboxes count

    Thanks 4x2y, how i can check for each checked items in the listview when I save the checked item in a module level variable?

  10. #10
    Frenzied Member
    Join Date
    Sep 06
    Posts
    1,258

    Re: Listview checkboxes count

    Look in above code and you will find the answer under Button1_Click sub.

  11. #11
    Hyperactive Member
    Join Date
    Sep 10
    Posts
    309

    Re: Listview checkboxes count

    Sorry, i have a look and it is not there. I would get something like this:

    Code:
    If CheckedItems.Checked Then
      For Each item As ListViewItem In CheckedItems
    	MessageBox.Show(CheckedItems.SubItems(1).Text)
      Next item
    End If

  12. #12
    Frenzied Member
    Join Date
    Sep 06
    Posts
    1,258

    Re: Listview checkboxes count

    I'm talking about this one
    vb Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         If mlviLatestCheckedItem.Checked Then
    3.             MsgBox(mlviLatestCheckedItem.Text & " has been checked")
    4.         Else
    5.             MsgBox(mlviLatestCheckedItem.Text & " has been unchecked")
    6.         End If
    7.     End Sub

  13. #13
    Hyperactive Member
    Join Date
    Sep 10
    Posts
    309

    Re: Listview checkboxes count

    Yeah, but when I select for each listview checked items, how do i get the messagebox display?

    I tried to use this:

    Code:
    If CheckedItems.Checked Then
      For Each item As ListViewItem In CheckedItems.Checked
    	MessageBox.Show(CheckedItems.SubItems(1).Text)
      Next item
    End If
    I get an error: for each statement cannot operate on variables of type 'bool'

    Any idea how to fix it?

  14. #14
    Frenzied Member
    Join Date
    Sep 06
    Posts
    1,258

    Re: Listview checkboxes count

    vb Code:
    1. For Each item As ListViewItem In ListView1.CheckedItems
    2.             If item.Checked Then
    3.                 MessageBox.Show(item.SubItems(1).Text)
    4.             End If
    5.         Next item

  15. #15
    Hyperactive Member
    Join Date
    Sep 10
    Posts
    309

    Re: Listview checkboxes count

    Thanks 4x2y, but I have got a problem. When I click the checkboxes in the listview, I can only get the messagebox display once a time when I click more than one checkboxes which I have ticked the item 1 checkbox to false and the item 2 checkbox to true.

    How i can get the messagebox to display more than one when I click on each checkboxes?

  16. #16
    Frenzied Member
    Join Date
    Sep 06
    Posts
    1,258

    Re: Listview checkboxes count

    I gave you code for displaying msg when check/uncheck item (post #9), and code to iterate checked items (post #15)

    Sorry, i don't understand what exactly you want!

    I will be offline soon, so, sorry if i don't reply fast.

  17. #17
    Hyperactive Member
    Join Date
    Sep 10
    Posts
    309

    Re: Listview checkboxes count

    Well when I click on the checkbox item 1 to set to false and checkbox item 2 to set to true, I can only ge the messagebox that says the checkbox 2 is set to true which it should display the messagebox twice to say that the checkbox item 1 is set to false and checkbox item 2 is set to true.

    When I click on each checkbox in the listview, I can only get the messagebox to display once at a time when one of the checkbox is set to false.

    How i can display it more than one when I click on the checkboxes whether if it set to true and false?

  18. #18
    Frenzied Member
    Join Date
    Sep 06
    Posts
    1,258

    Re: Listview checkboxes count

    Quote Originally Posted by PillKilla View Post
    Well when I click on the checkbox item 1 to set to false and checkbox item 2 to set to true, I can only ge the messagebox that says the checkbox 2 is set to true which it should display the messagebox twice to say that the checkbox item 1 is set to false and checkbox item 2 is set to true.
    This is difficult situation because you will need to store each item's check state in a collection then when you click the button, iterate collection and compare to current items' check state to see which item its check state changed.

Posting Permissions

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