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 -
Viewer.Items(i).ToString
-
The thing is:
An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in system.windows.forms.dllAdditional information: Specified argument was out of the range of valid values.
When running this code:
For i = 0 To ListView1.Items.Count
If ListView1.Items(i).ToString = splNick(0) Then
ListView1.Items.RemoveAt(i)
End If
Next iThat problem haunts me, whatever I do it's there.
-
Thansk, it actually worked

-
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. -
Thanks, It works without any problemems.
I edited the code so if it finds strName it should delete it because the application-program reads from a text file and adds the the names listed there, but the in the text file the name is referred plenty of times so there's a whole list of the same name, but it won't work, I don't know if it's because there is also included SubItems:
Name Phone Email
Christoffer Nokia christoffer@something.com
^ that name and with the same subitems is added more than ones because of the text file and I need to delete them but my code won't work:
For i = 0 To (ListView1.Items.Count - 1)
If ListView1.Items(i).ToString = strName Then
ListView1.Items.RemoveAt(i)
End If
Next i
-
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.
Thread Closed
This thread is kinda stale and has been closed but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.