Dim textcursor As Cursor
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ListBox1.Items.Add("apples")
ListBox1.Items.Add("peaches")
ListBox1.Items.Add("oranges")
ListBox1.Items.Add("bananas")
End Sub
Sub drawcursor(ByVal text As String, ByVal fnt As Font)
Dim bmp As New Bitmap(1, 1)
Dim g As Graphics = Graphics.FromImage(bmp)
Dim sz As SizeF = g.MeasureString(text, fnt)
bmp = New Bitmap(CInt(sz.Width), CInt(sz.Height))
g = Graphics.FromImage(bmp)
g.Clear(Color.White)
g.DrawString(text, fnt, Brushes.Black, 0, 0)
g.Dispose()
textcursor = New Cursor(bmp.GetHicon)
bmp.Dispose()
End Sub
Private Sub ListBox1_GiveFeedback(ByVal sender As Object, ByVal e As System.Windows.Forms.GiveFeedbackEventArgs) _
Handles ListBox1.GiveFeedback
If e.Effect = DragDropEffects.Move Then
e.UseDefaultCursors = False
System.Windows.Forms.Cursor.Current = textcursor
End If
End Sub
Private Sub ListBox2_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox2.DragEnter
If (e.Data.GetDataPresent(DataFormats.Text)) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub ListBox1_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) _
Handles ListBox1.DragOver
If e.Data.GetDataPresent(DataFormats.Text) Then
e.Effect = DragDropEffects.Move
ListBox1.SelectedIndex = ListBox1.IndexFromPoint(ListBox1.PointToClient(New Point(e.X, e.Y)))
End If
End Sub
Private Sub ListBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown
drawcursor(ListBox1.Text, ListBox1.Font)
ListBox1.DoDragDrop(ListBox1.Text, DragDropEffects.Copy Or DragDropEffects.Move)
End Sub
Private Sub ListBox2_GiveFeedback(ByVal sender As Object, ByVal e As System.Windows.Forms.GiveFeedbackEventArgs) _
Handles ListBox2.GiveFeedback
If e.Effect = DragDropEffects.Move Then
e.UseDefaultCursors = False
System.Windows.Forms.Cursor.Current = textcursor
End If
End Sub
Private Sub ListBox2_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles ListBox2.MouseDown
Dim ix As Integer = ListBox2.IndexFromPoint(e.Location)
If ix <> -1 Then
drawcursor(ListBox2.Items(ix).ToString, ListBox2.Font)
ListBox2.DoDragDrop(ix.ToString, DragDropEffects.Move)
'ListBox2.IndexFromPoint(ListBox2.PointToClient(New Point(e.X, e.Y)))
End If
End Sub
Private Sub ListBox2_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) _
Handles ListBox2.DragOver
If e.Data.GetDataPresent(DataFormats.Text) Then
e.Effect = DragDropEffects.Move
ListBox2.SelectedIndex = ListBox2.IndexFromPoint(ListBox2.PointToClient(New Point(e.X, e.Y)))
End If
End Sub
Private Sub ListBox2_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) _
Handles ListBox2.DragDrop
ListBox2.Items.Add(e.Data.GetData(DataFormats.Text).ToString)
If e.Data.GetDataPresent(DataFormats.Text) Then
Dim dix As Integer = CInt(e.Data.GetData(DataFormats.Text))
Dim ix As Integer = ListBox2.IndexFromPoint(ListBox2.PointToClient(New Point(e.X, e.Y)))
If ix <> -1 Then
Dim obj As Object = ListBox2.Items(dix)
ListBox2.Items.RemoveAt(dix)
ListBox2.Items.Insert(ix, obj)
ListBox2.SelectedIndex = ix
End If
End If
End Sub
Private Sub ListBox2_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) _
Handles ListBox2.KeyUp
Dim ix As Integer = ListBox2.SelectedIndex
If e.KeyCode = Keys.Delete And ix <> -1 Then
ListBox2.Items.RemoveAt(ix)
End If
End Sub