Drag and drop between two ListBox
Imports System.Drawing.Drawing2D Imports System Imports System.Drawing.Text Imports System.Drawing Imports System.Windows.Forms Imports System.Math Public Class MainClass Shared Sub Main() Dim form1 As Form = New Form1() Application.Run(form1) End Sub End Class Public Class Form1 Private m_DragSource As ListBox = Nothing Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load lstUnselected.AllowDrop = True lstSelected.AllowDrop = True ' Add event handlers. AddHandler lstUnselected.MouseDown, AddressOf List_MouseDown AddHandler lstUnselected.DragOver, AddressOf List_DragOver AddHandler lstUnselected.DragDrop, AddressOf List_DragDrop AddHandler lstUnselected.DragLeave, AddressOf List_DragLeave AddHandler lstSelected.MouseDown, AddressOf List_MouseDown AddHandler lstSelected.DragOver, AddressOf List_DragOver AddHandler lstSelected.DragDrop, AddressOf List_DragDrop AddHandler lstSelected.DragLeave, AddressOf List_DragLeave End Sub Private Sub List_MouseDown(ByVal sender As Object, _ ByVal e As System.Windows.Forms.MouseEventArgs) ' Select the item at this point. Dim this_list As ListBox = DirectCast(sender, ListBox) this_list.SelectedIndex = this_list.IndexFromPoint(e.X, e.Y) If this_list.SelectedIndex < 0 Then Exit Sub m_DragSource = this_list this_list.DoDragDrop( _ this_list.SelectedItem.ToString, _ DragDropEffects.Move) m_DragSource = Nothing End Sub ' Highlight the item under the mouse. Private Sub List_DragOver(ByVal sender As Object, _ ByVal e As System.Windows.Forms.DragEventArgs) If m_DragSource Is Nothing Then Exit Sub e.Effect = DragDropEffects.Move Dim this_list As ListBox = DirectCast(sender, ListBox) Dim pt As Point = _ this_list.PointToClient(New Point(e.X, e.Y)) Dim drop_index As Integer = _ this_list.IndexFromPoint(pt.X, pt.Y) this_list.SelectedIndex = drop_index End Sub Private Sub List_DragLeave(ByVal sender As Object, ByVal e As System.EventArgs) Dim this_list As ListBox = DirectCast(sender, ListBox) this_list.SelectedIndex = -1 End Sub ' Accept the drop. Private Sub List_DragDrop(ByVal sender As Object, _ ByVal e As System.Windows.Forms.DragEventArgs) Dim this_list As ListBox = DirectCast(sender, ListBox) MoveItem(e.Data.GetData(DataFormats.Text).ToString, _ m_DragSource, this_list, e.X, e.Y) End Sub ' Move the value txt from drag_source to drop_target. Private Sub MoveItem(ByVal txt As String, ByVal drag_source As ListBox, _ ByVal drop_target As ListBox, ByVal X As Integer, ByVal Y As Integer) Dim drop_index As Integer = drop_target.SelectedIndex If drop_index < 0 Then drop_index = drop_target.Items.Add(txt) Else drop_target.Items.Insert(drop_target.SelectedIndex, txt) End If drop_target.SelectedIndex = drop_index If drag_source Is drop_target Then Dim target_index As Integer = drag_source.FindStringExact(txt) If target_index = drop_index Then _ target_index = drag_source.FindStringExact(txt, target_index) drag_source.Items.RemoveAt(target_index) Else drag_source.Items.Remove(txt) End If End Sub End Class <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _ Partial Public Class Form1 Inherits System.Windows.Forms.Form 'Form overrides dispose to clean up the component list. <System.Diagnostics.DebuggerNonUserCode()> _ Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing AndAlso components IsNot Nothing Then components.Dispose() End If MyBase.Dispose(disposing) End Sub 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. <System.Diagnostics.DebuggerStepThrough()> _ Private Sub InitializeComponent() Me.Label2 = New System.Windows.Forms.Label Me.Label1 = New System.Windows.Forms.Label Me.lstSelected = New System.Windows.Forms.ListBox Me.lstUnselected = New System.Windows.Forms.ListBox Me.SuspendLayout() ' 'Label2 ' Me.Label2.Location = New System.Drawing.Point(152, 0) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(144, 16) Me.Label2.TabIndex = 11 Me.Label2.Text = "Right" Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ' 'Label1 ' Me.Label1.Location = New System.Drawing.Point(0, 0) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(144, 16) Me.Label1.TabIndex = 10 Me.Label1.Text = "Left" Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ' 'lstSelected ' Me.lstSelected.FormattingEnabled = True Me.lstSelected.Items.AddRange(New Object() {"1", "2", "3", "4", "5", "6", "7", "8"}) Me.lstSelected.Location = New System.Drawing.Point(152, 16) Me.lstSelected.Name = "lstSelected" Me.lstSelected.Size = New System.Drawing.Size(144, 264) Me.lstSelected.TabIndex = 9 ' 'lstUnselected ' Me.lstUnselected.FormattingEnabled = True Me.lstUnselected.Items.AddRange(New Object() {"A", "B", "C", "D", "E", "F", "G", "H"}) Me.lstUnselected.Location = New System.Drawing.Point(0, 16) Me.lstUnselected.Name = "lstUnselected" Me.lstUnselected.Size = New System.Drawing.Size(144, 264) Me.lstUnselected.TabIndex = 8 ' 'Form1 ' Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(297, 281) Me.Controls.Add(Me.Label2) Me.Controls.Add(Me.Label1) Me.Controls.Add(Me.lstSelected) Me.Controls.Add(Me.lstUnselected) Me.Name = "Form1" Me.Text = "DragBetweenListBoxes" Me.ResumeLayout(False) End Sub Friend WithEvents Label2 As System.Windows.Forms.Label Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents lstSelected As System.Windows.Forms.ListBox Friend WithEvents lstUnselected As System.Windows.Forms.ListBox End Class