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
Dim textcursor As Cursor
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 ListBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles ListBox1.MouseDown
Dim ix As Integer = ListBox1.IndexFromPoint(e.Location)
If ix <> -1 Then
drawcursor(ListBox1.Items(ix).ToString, ListBox1.Font)
ListBox1.DoDragDrop(ix.ToString, DragDropEffects.Move)
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_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) _
Handles ListBox1.DragDrop
If e.Data.GetDataPresent(DataFormats.Text) Then
Dim dix As Integer = CInt(e.Data.GetData(DataFormats.Text))
Dim ix As Integer = ListBox1.IndexFromPoint(ListBox1.PointToClient(New Point(e.X, e.Y)))
If ix <> -1 Then
Dim obj As Object = ListBox1.Items(dix)
ListBox1.Items.RemoveAt(dix)
ListBox1.Items.Insert(ix, obj)
ListBox1.SelectedIndex = ix
End If
End If
End Sub