Posted By: henningms | May 20th, 2004 @ 1:41 PM
page 1 of 1
Comments: 9 | Views: 6366
Hi again, I have another problem that needs to be solved.
How do you loop through a ListView and check if strName is the text of one item?

Dim i As Integer
For i = 0 To ListView1.Items.Count
   If ListView1.Items(i).Text = strName Then
      MsgBox "Matches"
   End If
Next i

Please help, I am getting mad about this.
Have you tried: (untested)

ListView1.Item(i).Value = strName
or
ListView1.Items(i) = strName
lol! Try this:

For i = 0 To (ListView1.Items.Count -1)

I really can't believe I didn't pick this up the first time around.. I guess I was looking for something a little more complex.
Wouldn't this have been easier, to avoid using an index in the first place:

For Each itm as ListItem In ListView1.Items
   If itm.Text = strName Then
      MsgBox "Matches"
   End If
Next itm

Also, assuming you only want to get the first, and possibly only, match, you can insert in an:
  Exit For
after the MsgBox.
Even simpler still:

I like using the .IndexOf() method.

Dim myItem As New ListViewItem("matchstring")

ListView1.Items.RemoveAt(ListView1.Items.IndexOf(myItem))


I guess I should mention that it isn't good policy to do the above in-line like that, instead get the IndexOf value and check for <> -1 then remove.

page 1 of 1
Comments: 9 | Views: 6366