Posted By

ed_trench on 09/29/12


Tagged

VBNet controls listview


Versions (?)

Copy items between 2 ListView Controls


 / Published in: VB.NET
 

Copying/Moving items between 2 ListView Controls for reference

  1. Public Class frmFamilyMembers
  2.  
  3. Private Sub frmFamilyMembers_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  4.  
  5. Dim familyPerson1 As New FamilyPerson With {.Forename = "Sarah",
  6. .Surname = "Trenchard",
  7. .DOB = "26/04/1978"}
  8.  
  9. Dim familyPerson2 As New FamilyPerson With {.Forename = "Ed",
  10. .Surname = "Trenchard",
  11. .DOB = "13/08/1977"}
  12.  
  13.  
  14. Dim familyPerson3 As New FamilyPerson With {.Forename = "Kay",
  15. .Surname = "Trenchard",
  16. .DOB = "04/05/1955"}
  17.  
  18.  
  19. Dim familyPerson4 As New FamilyPerson With {.Forename = "Caroline",
  20. .Surname = "Turner",
  21. .DOB = "31/10/1976"}
  22.  
  23. Dim familyPerson5 As New FamilyPerson With {.Forename = "Alan",
  24. .Surname = "Turner",
  25. .DOB = "01/05/1950"}
  26.  
  27. Dim familyPerson6 As New FamilyPerson With {.Forename = "Lynne",
  28. .Surname = "Turner",
  29. .DOB = "12/12/1940"}
  30.  
  31.  
  32. Dim leftSidePersons As New List(Of FamilyPerson)
  33. Dim rightSidePersons As New List(Of FamilyPerson)
  34.  
  35. leftSidePersons.Add(familyPerson1)
  36. leftSidePersons.Add(familyPerson2)
  37. leftSidePersons.Add(familyPerson3)
  38.  
  39. rightSidePersons.Add(familyPerson4)
  40. rightSidePersons.Add(familyPerson5)
  41. rightSidePersons.Add(familyPerson6)
  42.  
  43. With Me.lvwLeft
  44. .View = View.Details
  45. .GridLines = True
  46. .FullRowSelect = True
  47. .HideSelection = False
  48. .Columns.Add("Surname").Width = 150
  49. .Columns.Add("Forename").Width = 100
  50. .Columns.Add("DOB").Width = 70
  51. End With
  52.  
  53. For Each person In leftSidePersons
  54. Dim item As New ListViewItem
  55. With item
  56. .Text = person.Surname
  57. .SubItems.Add(person.Forename)
  58. .SubItems.Add(person.DOB.ToShortDateString)
  59. End With
  60. Me.lvwLeft.Items.Add(item)
  61. Next
  62.  
  63. With Me.lvwRight
  64. .View = View.Details
  65. .GridLines = True
  66. .FullRowSelect = True
  67. .HideSelection = False
  68. .Columns.Add("Surname").Width = 150
  69. .Columns.Add("Forename").Width = 100
  70. .Columns.Add("DOB").Width = 70
  71. End With
  72.  
  73. For Each person In rightSidePersons
  74. Dim item As New ListViewItem
  75. With item
  76. .Text = person.Surname
  77. .SubItems.Add(person.Forename)
  78. .SubItems.Add(person.DOB.ToShortDateString)
  79. End With
  80. Me.lvwRight.Items.Add(item)
  81. Next
  82.  
  83. End Sub
  84.  
  85. Private Sub btnCopy_Click(sender As Object, e As System.EventArgs) Handles btnCopy.Click
  86.  
  87. If Not Me.lvwLeft.SelectedItems.Count = 0 Then
  88.  
  89. Dim lvi As ListViewItem = Me.lvwLeft.SelectedItems(0)
  90. Dim lvi2 As ListViewItem = CType(lvi.Clone, ListViewItem)
  91.  
  92. Me.lvwRight.Items.Insert(0, lvi2)
  93. Me.lvwLeft.Items.Remove(Me.lvwLeft.SelectedItems(0))
  94.  
  95. End If
  96. End Sub
  97.  
  98. Private Sub btnCopyBack_Click(sender As Object, e As System.EventArgs) Handles btnCopyBack.Click
  99.  
  100. If Not Me.lvwRight.SelectedItems.Count = 0 Then
  101.  
  102. Dim lvi As ListViewItem = Me.lvwRight.SelectedItems(0)
  103. Dim lvi2 As ListViewItem = CType(lvi.Clone, ListViewItem)
  104.  
  105. Me.lvwLeft.Items.Insert(0, lvi2)
  106. Me.lvwRight.Items.Remove(Me.lvwRight.SelectedItems(0))
  107.  
  108. End If
  109.  
  110. End Sub
  111.  
  112. End Class
  113.  
  114. Public Class FamilyPerson
  115.  
  116. Public Property Forename As String
  117. Public Property Surname As String
  118. Public Property DOB As Date
  119.  
  120.  
  121. End Class

Report this snippet  

You need to login to post a comment.