Listview item add in loop

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rr

    Listview item add in loop

    I have a list view with several rows and would like to populate it using a
    loop. However when I try to use a variable in the obejct name it fails and
    says obejct
    doest exist. Is there some object id cat operator?

    Here is a snippet:

    Dim item1 As New ListViewItem("s tuff1")
    Dim item2 As New ListViewItem("s tuff2")
    Dim item3 As New ListViewItem("s tuff3")
    Dim item4 As New ListViewItem("s tuff4")

    Dim i as Integer

    for i=0

    If Not PingPC(item + cstr(i).Text) Then
    RowColor(item + cstr (i))
    Else
    InsertIp(item + cstr (i))
    End If

    next

    It fails stating there is no itemi, I have also tried item + cstr(i)
    Any suggestions or otyher ways to populate a listview.

    thanks
    Robert


  • Fergus Cooney

    #2
    Re: Listview item add in loop

    Hi Robert.

    Rather than use a set of independant Item variables, why not use an array?

    Dim item (3) As ListViewItem
    item (0) = New ListViewItem("s tuff1")
    item (1) = New ListViewItem("s tuff2")
    item (2) = New ListViewItem("s tuff3")
    item (3) = New ListViewItem("s tuff4")

    for i=0
    If Not PingPC(item (i).Text) Then
    RowColor(item (i))
    Else
    InsertIp(item (i))
    End If
    next

    I noticed that your question refers to populating the ListView with a loop
    but that your code doesn't. I have followed the code assuming, lol, that the
    'documentation' is out of synch.

    If you want "stuff1", "stuff", etc inside a loop, you'll have to let me
    know.

    Regards,
    Fergus


    Comment

    • Cor

      #3
      Re: Listview item add in loop

      Hi Rolls Roys,

      In addition to Fergus, here a sample of a way to do a for each loop in a
      listview

      Dim vItem As ListViewItem
      Dim vcolor As System.Drawing. Color = System.Drawing. Color.Blue
      For Each vItem In ListView1.Items
      If vcolor.ToString = System.Drawing. Color.Blue.ToSt ring Then
      vcolor = System.Drawing. Color.Red
      Else
      vcolor = System.Drawing. Color.Blue
      End If
      vItem.SubItems( 0).BackColor = vcolor
      Next


      I hope this helps a little bit
      Cor


      Comment

      • rr

        #4
        Re: Listview item add in loop

        Thanks for the replies, I will give them a try on Monday.

        The code was a bitt off as it was late Friday and I took the loop out
        earlier because it didnt work. I wrote it back in from memroy.

        I am trying to populate the listviews first column with static data. The
        other other colums are dynamic data from functions..

        thanks for your help
        robert


        "Cor" <non@non.com> wrote in message
        news:3f87e2ed$0 $32029$48b97d01 @reader20.wxs.n l...[color=blue]
        > Hi Rolls Roys,
        >
        > In addition to Fergus, here a sample of a way to do a for each loop in a
        > listview
        >
        > Dim vItem As ListViewItem
        > Dim vcolor As System.Drawing. Color = System.Drawing. Color.Blue
        > For Each vItem In ListView1.Items
        > If vcolor.ToString = System.Drawing. Color.Blue.ToSt ring Then
        > vcolor = System.Drawing. Color.Red
        > Else
        > vcolor = System.Drawing. Color.Blue
        > End If
        > vItem.SubItems( 0).BackColor = vcolor
        > Next
        >
        >
        > I hope this helps a little bit
        > Cor
        >
        >[/color]


        Comment

        Working...