Public Class Form1
'Listbox之间项目拖动示例,左键移动,右键复制
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ListBox2.AllowDrop = True
End Sub
Private Sub ListBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown
Dim DragIndex = ListBox1.IndexFromPoint(e.X, e.Y)
If DragIndex <> ListBox.NoMatches Then
ListBox1.SelectedIndex = DragIndex
If e.Button = Windows.Forms.MouseButtons.Left Then
DoDragDrop(ListBox1.Items(DragIndex), DragDropEffects.Move)
ElseIf e.Button = Windows.Forms.MouseButtons.Right Then
DoDragDrop(ListBox1.Items(DragIndex), DragDropEffects.Copy)
End If
End If
End Sub
Private Sub ListBox2_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox2.DragEnter
e.Effect = e.AllowedEffect
End Sub
Private Sub ListBox2_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox2.DragDrop
Dim item As Object = CType(e.Data.GetData(GetType(System.String)), System.Object)
Dim item2 As Integer = ListBox2.IndexFromPoint(ListBox2.PointToClient(New Point(e.X, e.Y)))
If item2 = -1 Then
ListBox2.Items.Add(item)
Else
ListBox2.Items.Insert(item2, item)
End If
If e.AllowedEffect = DragDropEffects.Move Then
ListBox1.Items.Remove(item)
End If
End Sub
End Class