Check each listview checkbox
Hi guys,
I am working on my listview as I have almost finish it, but there is a problem with the checkboxes. When I run my form on a debug, I have the first row are already set to true and when click on the second row in the listview to set the checkbox to true while it will add the subitems in the label, so when I click on a button, it will say that the first row have set to true which I have only select on a second row in the listview.
Here's the code:
Code:
Private Function GetEnabledDisabledText(ByVal enabled As Boolean) As String
Return (If(enabled, "Enabled", "Disabled"))
End Function
Private Shared subItems1 As New List(Of String)()
Private Sub listView1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Dim strItem As String = listView1.HitTest(e.Location).Item.SubItems(1).Text
If subItems1.Contains(strItem) Then
subItems1.Remove(strItem)
Else
subItems1.Add(listView1.HitTest(e.Location).Item.SubItems(1).Text)
End If
label3.Text = String.Join(", ", subItems1.ToArray())
End Sub
Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
button1.Enabled = False
Dim a() As String = Nothing
a = label3.Text.Split(","c)
Dim j As Integer = 0
Dim str As Object = GetEnabledDisabledText(listView1.Items(j).Checked)
MessageBox.Show(a(j).Trim() & " is " & str.ToString())
End Sub
Do you know how I can get the messagebox to display and tells me which rows in the listview that I have select and whether if the checbox is set to true or false after I add the subitems in the label?
Can anyone help me with this?
Re: Check each listview checkbox
You should not be handling the MouseClick event. The ListView has ItemCheck and ItemChecked events. If you care about checking items then those are the obvious events to handle. I'll let you read the documentation for each to determine which is better in your case and what information it provides. If you want to get information regarding the items that are checked at any time, you simply loop through the CheckedItems collection.
Re: Check each listview checkbox
Ok so do i need to change from mouseclick event to itemcheck event and do i need to change the code from below?
From this:
Code:
Dim strItem As String = listView1.HitTest(e.Location).Item.SubItems(1).Text
To this:
Code:
Dim strItem As String = listView1.HitTest(e.NewValue).Item.SubItems(1).Text
Re: Check each listview checkbox
You do need to change the code you have but not to the code you suggest. Also, given that ItemCheck occurs before the item is checked and ItemChecked occurs after, are you sure that's the correct event? If you've read the documentation for the event then you know that it tells you which item is being checked, so what's the HitTest for? Even if you were going to call HitTest, given that it gives you information about a Point, how could you pass it a Boolean?