|
-
May 4th, 2012, 11:26 AM
#1
Thread Starter
Hyperactive Member
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
-
May 4th, 2012, 11:49 AM
#2
Re: Listview checkboxes count
Code:
Dim cItems As ListView.CheckedListViewItemCollection = Me.ListView1.CheckedItems
MsgBox(cItems.Count)
-
May 4th, 2012, 11:50 AM
#3
Re: Listview checkboxes count
Try this
vb 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 & " is checked.") End If Next End Sub
-
May 4th, 2012, 12:03 PM
#4
Re: Listview checkboxes count
vb.net Code:
Dim query = TestListView.Items.Cast(Of ListViewItem)() Dim checked = query.Where(Function(item) item.Selected = True).Count() 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.
-
May 4th, 2012, 05:44 PM
#5
Thread Starter
Hyperactive Member
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?
-
May 4th, 2012, 05:57 PM
#6
Re: Listview checkboxes count
Put your code inside ItemChecked event
vb Code:
Private Sub ListView1_ItemChecked(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckedEventArgs) Handles ListView1.ItemChecked If e.Item.Checked Then MsgBox(e.Item.Text & " has been checked") Else MsgBox(e.Item.Text & " has been unchecked") End If End Sub
-
May 4th, 2012, 06:28 PM
#7
Thread Starter
Hyperactive Member
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?
-
May 4th, 2012, 06:36 PM
#8
Re: Listview checkboxes count
Save the checked item in a module level variable and use it in button click event, like this
vb Code:
Public Class Form1 Private mlviLatestCheckedItem As ListViewItem Private Sub ListView1_ItemChecked(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckedEventArgs) Handles ListView1.ItemChecked mlviLatestCheckedItem = e.Item End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If mlviLatestCheckedItem.Checked Then MsgBox(mlviLatestCheckedItem.Text & " has been checked") Else MsgBox(mlviLatestCheckedItem.Text & " has been unchecked") End If End Sub End Class
-
May 4th, 2012, 06:53 PM
#9
Thread Starter
Hyperactive Member
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?
-
May 4th, 2012, 06:55 PM
#10
Re: Listview checkboxes count
Look in above code and you will find the answer under Button1_Click sub.
-
May 4th, 2012, 06:59 PM
#11
Thread Starter
Hyperactive Member
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
-
May 4th, 2012, 07:05 PM
#12
Re: Listview checkboxes count
I'm talking about this one
vb Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If mlviLatestCheckedItem.Checked Then MsgBox(mlviLatestCheckedItem.Text & " has been checked") Else MsgBox(mlviLatestCheckedItem.Text & " has been unchecked") End If End Sub
-
May 4th, 2012, 07:39 PM
#13
Thread Starter
Hyperactive Member
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?
-
May 4th, 2012, 07:45 PM
#14
Re: Listview checkboxes count
vb Code:
For Each item As ListViewItem In ListView1.CheckedItems If item.Checked Then MessageBox.Show(item.SubItems(1).Text) End If Next item
-
May 4th, 2012, 08:03 PM
#15
Thread Starter
Hyperactive Member
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?
-
May 4th, 2012, 08:11 PM
#16
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.
-
May 4th, 2012, 08:15 PM
#17
Thread Starter
Hyperactive Member
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?
-
May 4th, 2012, 08:43 PM
#18
Re: Listview checkboxes count
 Originally Posted by PillKilla
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|