Coffeehouse Thread

10 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Help needed - Visual Basic .NET

Back to Forum: Coffeehouse
  • User profile image
    henningms

    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.

  • User profile image
    sbc

    Have you tried: (untested)

    ListView1.Item(i).Value = strName
    or
    ListView1.Items(i) = strName

  • User profile image
    Manip

    Viewer.Items(i).ToString

  • User profile image
    henningms

    The thing is:

    An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in system.windows.forms.dll

    Additional 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 i

    That problem haunts me, whatever I do it's there.

  • User profile image
    henningms

    Thansk, it actually worked Smiley

  • User profile image
    Manip

    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.

  • User profile image
    gregoryw

    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.

  • User profile image
    henningms

     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


     

  • User profile image
    gmiley

    Even simpler still:

    I like using the .IndexOf() method.

    Dim myItem As New ListViewItem("matchstring")

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


  • User profile image
    gmiley

    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.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.