Compare Listview Items and return the index
-
Hello everyone, I was wondering if anyone out there might know how to compare an entire listview item and return the index if one of the items matches. i have tried:
Public Function FindItem(ByVal item As ListViewItem) As Integer Dim itm As ListViewItem For Each itm In Me.Items If itm.Equals(item) Then Return itm.Index Exit Function End If Next Return -1 End Function AND Public Function FindItem(ByVal item As ListViewItem) As Integer Dim itm As ListViewItem For Each itm In Me.Items If itm.Text = item.text Then Return itm.Index Exit Function End If Next Return -1 End Function
The first one does not seem to work at all, and the second one works, but only checks the text value of the first column and does not seem to compare other column values of the listviewitem. Thank you eatwork -
Hello everyone, I was wondering if anyone out there might know how to compare an entire listview item and return the index if one of the items matches. i have tried:
Public Function FindItem(ByVal item As ListViewItem) As Integer Dim itm As ListViewItem For Each itm In Me.Items If itm.Equals(item) Then Return itm.Index Exit Function End If Next Return -1 End Function AND Public Function FindItem(ByVal item As ListViewItem) As Integer Dim itm As ListViewItem For Each itm In Me.Items If itm.Text = item.text Then Return itm.Index Exit Function End If Next Return -1 End Function
The first one does not seem to work at all, and the second one works, but only checks the text value of the first column and does not seem to compare other column values of the listviewitem. Thank you eatworkeatwork wrote:
the second one works, but only checks the text value of the first column and does not seem to compare other column values of the listviewitem
That's because you did not write the code in such a way that it compares the values of the sub-items. ;P You'll need to iterate the SubItems collection of each ListViewItem, and compare their Text values. Josh
-
eatwork wrote:
the second one works, but only checks the text value of the first column and does not seem to compare other column values of the listviewitem
That's because you did not write the code in such a way that it compares the values of the sub-items. ;P You'll need to iterate the SubItems collection of each ListViewItem, and compare their Text values. Josh