Hi guys! I need some help. I have two listviews that uses drag and drop to pass values, however I can only pass the value of my first column. What I need is to pass the value of my two columns. Thanks! God Bless.! :)
paulablanca 0 Junior Poster in Training
Unhnd_Exception
You should be able to copy and paste this into a new project.
Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Adds to listview items to the form
Me.Size = New Size(400, 400)
Me.Controls.Clear()
Dim Listview1 As New ListView
Dim Listview2 As New ListView
Listview1.Location = New Point(50, 50)
Listview1.Name = "ListView1"
Listview2.Location = New Point(200, 50)
Listview2.Name = "ListView2"
Me.Controls.Add(Listview1)
Me.Controls.Add(listview2)
For Each c As Control In Me.Controls
If TypeOf c Is ListView Then
With CType(c, ListView)
.AllowDrop = True
.Columns.Add("Name")
.Columns.Add("Type")
.FullRowSelect = True
.MultiSelect = False
.Size = New Size(150, 300)
.View = View.Details
AddHandler .ItemDrag, AddressOf ListView_ItemDrag
AddHandler .DragEnter, AddressOf ListView_DragEnter
AddHandler .DragDrop, AddressOf ListView_DragDrop
.Items.Add(New ListViewItem(New String() {c.Name, 1}))
End With
End If
Next
End Sub
Private Sub ListView_ItemDrag(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemDragEventArgs)
If sender Is Nothing OrElse Not TypeOf sender Is ListView Then Exit Sub
With CType(sender, ListView)
.DoDragDrop(e.Item, DragDropEffects.Move)
End With
End Sub
Private Sub ListView_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
If sender Is Nothing OrElse Not TypeOf sender Is ListView Then Exit Sub
'If this is a listview item then allow the drag
If e.Data.GetDataPresent(GetType(ListViewItem)) Then
e.Effect = DragDropEffects.Move
End If
End Sub
Private Sub ListView_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
If sender Is Nothing OrElse Not TypeOf sender Is ListView Then Exit Sub
'Remove the item from the current listview and drop it in the new listview
With CType(sender, ListView)
If e.Data.GetDataPresent(GetType(ListViewItem)) Then
Dim draggedItem As ListViewItem = CType(e.Data.GetData(GetType(ListViewItem)), ListViewItem)
draggedItem.ListView.Items.Remove(draggedItem)
.Items.Add(draggedItem)
End If
End With
End Sub
End Class
If this solves your problem Mark it as solved. If, not say why it didn't solve your problem
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.