Results 1 to 5 of 5

Thread: VB.NET 2005 - Copy ListView to ListView

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Posts
    369

    VB.NET 2005 - Copy ListView to ListView

    Hi,

    VB.NET 2005

    Anyone has a quick way to copy a listview to a new listview?

    It tried the following, but it crashes ..

    Dim arrTemp(lvwTemp.Items.Count) As ListViewItem
    lvwTemp.Items.CopyTo(arrTemp, 0)
    lvwObjectsFound.Items.AddRange(arrTemp)


    Thanks for helping.


    Structure of lvwTemp is:

    With lvwTemp
    .Clear()
    .Columns.Add("Nom", 400, HorizontalAlignment.Left)
    .Columns.Add("Type", 200, HorizontalAlignment.Left)
    End With

  2. #2
    Frenzied Member
    Join Date
    May 2006
    Location
    Toronto, ON
    Posts
    1,093

    Re: VB.NET 2005 - Copy ListView to ListView

    Code:
    For Each itm As ListViewItem In lvwTemp.Items
    
        lvwObjectsFound.Items.Add(itm)
    
    Next

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: VB.NET 2005 - Copy ListView to ListView

    You can't add a ListViewItem to a ListView when it's already in another. You need to call the Clone method of each item to create a copy you can then add to the other ListView. As the documentation for the ListViewItem.Clone method says:
    You can use this method to create a new instance of the ListViewItem class based on an existing item. Even the subitems of the item being cloned are specified for the new version. This feature is useful if you want to reuse a ListViewItem in more than one ListView control.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    New Member
    Join Date
    Aug 2011
    Posts
    1

    Re: VB.NET 2005 - Copy ListView to ListView

    Quote Originally Posted by Tom Sawyer View Post
    Code:
    For Each itm As ListViewItem In lvwTemp.Items
    
        lvwObjectsFound.Items.Add(itm.Clone())
    
    Next
    http://msdn.microsoft.com/en-us/libr...tem.clone.aspx

    Have fun!

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: VB.NET 2005 - Copy ListView to ListView

    Quote Originally Posted by vinhnd View Post
    The fun was had over 3 1/2 years ago.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width