VB.NET - Code Sample - Custom DragIcon

Set the mouse cursor icon when performing a drag and drop operation in VB.net


In order to set the custom icon for use in dragging and dropping you must set the icon in the GiveFeedBack event of the object being dragged.In the example below a picturebox named PictureBox1 is being dragged. The AllowDrop property of the form must be set to true. The icon that the drag cursor icon is set to is the icon of the current form.

The Code:

    Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        PictureBox1.DoDragDrop(PictureBox1, DragDropEffects.Move)
    End Sub

    Private Sub Form1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles MyBase.DragEnter
        e.Effect = DragDropEffects.Move
    End Sub

    Private Sub PictureBox1_GiveFeedback(ByVal sender As Object, ByVal e As System.Windows.Forms.GiveFeedbackEventArgs) Handles PictureBox1.GiveFeedback
        e.UseDefaultCursors = False

        If ((e.Effect And DragDropEffects.Move) = DragDropEffects.Move) Then
            Cursor.Current = New Cursor(Me.Icon.Handle)
            Cursor.Current = System.Windows.Forms.Cursors.Default
        End If
    End Sub
About this page: