Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Why am I getting an error that says I can only add "First Item" in one spot or I must remove or clone it?

        Dim listViewItem As New ListViewItem()
       Dim ctr As Integer = 0

       lvw.View = View.Details
       lvw.Columns.Add("First Column", 100, HorizontalAlignment.Center)
       lvw.Columns.Add("Second Column", 100, HorizontalAlignment.Center)
       lvw.Columns.Add("Third Column", 100, HorizontalAlignment.Center)

       For ctr = 0 To 5
           listViewItem.Text = "First Item"
           listViewItem.SubItems.Add("Second Item")
           listViewItem.SubItems.Add("Third Item")
           lvw.Items.Add(listViewItem)
       Next

   End Sub

 

Am I adding the ListViewItem to the lvw wrong? If I move the lvw.Items.Add(listViewItem) outside the loop it works fine but obviously only does it once. Once I move it into the loop, it bombs.

Thanks,

Tehon

  • Moderators
Posted

Avoid naming a variable the same as the type (listViewItem)

 

Try this....

        lvw.Columns.Add("First Column", 100, HorizontalAlignment.Center)
       lvw.Columns.Add("Second Column", 100, HorizontalAlignment.Center)
       lvw.Columns.Add("Third Column", 100, HorizontalAlignment.Center)
       lvw.Columns.Add("Forth Column", 100, HorizontalAlignment.Center)
       lvw.Columns.Add("Fifth Column", 100, HorizontalAlignment.Center)

       Dim ctr As Integer
       For ctr = 1 To 10
           Dim item1 As New ListViewItem(ctr.ToString)
           item1.SubItems.Add("Two " & ctr.ToString)
           item1.SubItems.Add("Three " & ctr.ToString)
           item1.SubItems.Add("Four " & ctr.ToString)
           item1.SubItems.Add("Five " & ctr.ToString)
           lvw.Items.AddRange(New ListViewItem() {item1})
       Next

Visit...Bassic Software

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...