Listview item add in loop

R

rr

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("stuff1")
Dim item2 As New ListViewItem("stuff2")
Dim item3 As New ListViewItem("stuff3")
Dim item4 As New ListViewItem("stuff4")

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
 
F

Fergus Cooney

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("stuff1")
item (1) = New ListViewItem("stuff2")
item (2) = New ListViewItem("stuff3")
item (3) = New ListViewItem("stuff4")

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
 
C

Cor

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.ToString 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
 
R

rr

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top