none
How can i transfer the selected Data from Listview1 to listview2 in the same form? RRS feed

  • Question

  • hi

    i have two list view ( in the same form )

    how can i transfer the selected data from listview1 to listview2 ?

    Monday, May 7, 2018 11:46 AM

Answers

  • what should i write in listbox2

    Listbox:

    'For Copy
            For Each item In ListBox1.SelectedItems
                ListBox2.Items.Add(item)
            Next
    
    'For Transfer
            For i = ListBox1.SelectedItems.Count - 1 To 0 Step -1
                ListBox1.Items.Remove(ListBox1.SelectedItems(i))
            Next

    Listview:

    'For Copy For Each item As ListViewItem In ListView1.SelectedItems ListView2.Items.Add(item.Clone()) Next

    'For Transfer For i = ListView1.SelectedItems.Count - 1 To 0 Step -1 ListView1.Items.Remove(ListView1.SelectedItems(i)) Next


    • Marked as answer by anglefbi Monday, May 7, 2018 5:53 PM
    Monday, May 7, 2018 2:29 PM

All replies

  • Use SelectedItems and iterate then as shown in the following documentation for SelectedItems.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, May 7, 2018 12:11 PM
    Moderator
  • Looks like this question has been asked in the forum before:

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/acdf244e-837b-4f95-af25-d017400d12bc/vbnet-listview-clone?forum=vbgeneral


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Monday, May 7, 2018 12:22 PM
  • guys

    i need a short answer

    what should i write in listbox2

    because actually the second example very long

    Monday, May 7, 2018 12:57 PM
  • How much shorter does it need to be?

    Private Sub CopyListView(ByVal lv1 As ListView, ByVal lv2 As ListView)
      For Each lvi As ListViewItem In lv1.Items
        Dim newLvi As ListViewItem = lvi.Clone()
        For Each lvsi As ListViewItem.ListViewSubItem In lvi.SubItems
          newLvi.SubItems.Add(New ListViewItem.ListViewSubItem(newLvi, lvsi.Text, lvsi.ForeColor, lvsi.BackColor, lvsi.Font))
        Next
        lv2.Items.Add(newLvi)
      Next
    End Sub


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Monday, May 7, 2018 1:27 PM
  • what should i write in listbox2

    Listbox:

    'For Copy
            For Each item In ListBox1.SelectedItems
                ListBox2.Items.Add(item)
            Next
    
    'For Transfer
            For i = ListBox1.SelectedItems.Count - 1 To 0 Step -1
                ListBox1.Items.Remove(ListBox1.SelectedItems(i))
            Next

    Listview:

    'For Copy For Each item As ListViewItem In ListView1.SelectedItems ListView2.Items.Add(item.Clone()) Next

    'For Transfer For i = ListView1.SelectedItems.Count - 1 To 0 Step -1 ListView1.Items.Remove(ListView1.SelectedItems(i)) Next


    • Marked as answer by anglefbi Monday, May 7, 2018 5:53 PM
    Monday, May 7, 2018 2:29 PM
  • The problem is that you use a presentation class as a data class and therefore there is no short answer.

    The listview is in fact a simple control which has no databinding. 

    If you had solved your problem with a complex datacontrol likewise a datagridview, the answer did contain 2 rows. 

    A listview is created to show files and image content of that.  



    Success
    Cor

    Monday, May 7, 2018 4:12 PM
  • what should i write in listbox2

    Listbox:

    'For Copy
            For Each item In ListBox1.SelectedItems
                ListBox2.Items.Add(item)
            Next
    
    'For Transfer
            For i = ListBox1.SelectedItems.Count - 1 To 0 Step -1
                ListBox1.Items.Remove(ListBox1.SelectedItems(i))
            Next

    Listview:

    'For Copy For Each item As ListViewItem In ListView1.SelectedItems ListView2.Items.Add(item.Clone()) Next

    'For Transfer For i = ListView1.SelectedItems.Count - 1 To 0 Step -1 ListView1.Items.Remove(ListView1.SelectedItems(i)) Next


    thank you very very much

    god bless you 

    it helped me :D

    Monday, May 7, 2018 6:04 PM
  • How much shorter does it need to be?

    Private Sub CopyListView(ByVal lv1 As ListView, ByVal lv2 As ListView)
      For Each lvi As ListViewItem In lv1.Items
        Dim newLvi As ListViewItem = lvi.Clone()
        For Each lvsi As ListViewItem.ListViewSubItem In lvi.SubItems
          newLvi.SubItems.Add(New ListViewItem.ListViewSubItem(newLvi, lvsi.Text, lvsi.ForeColor, lvsi.BackColor, lvsi.Font))
        Next
        lv2.Items.Add(newLvi)
      Next
    End Sub


    Paul ~~~~ Microsoft MVP (Visual Basic)

    thanks paul

    Monday, May 7, 2018 6:04 PM
  • You can use the ListViews selected index changed event too.

        Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged
            ListView2.Items.Clear()
            For Each Item As ListViewItem In ListView1.SelectedItems
                ListView2.Items.Add(CType(Item.Clone, ListViewItem))
            Next
        End Sub


    La vida loca

    Monday, May 7, 2018 10:58 PM