MouseMove & DoubleClick

This is a VERY rough sample - but it serves the purpose. I’ve simply used a label and added the appropriate events (Note: I haven’t used the double click event and simply hijacked the mouse_down event - if you do have access to the double_click event, it is highly suggested you use it!)


 ' Boolean to determine whether a drag is being made
    Private DoDrag As Boolean = False

    Private Sub lblLabel1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lblLabel1.MouseDown
        ' Determine whether we are performing a drag operation OR a double click
        If e.Clicks = 1 Then
            DoDrag = True
        Else
            DoDrag = False
            MessageBox.Show("Double Click Made")
        End If

    End Sub

    Private Sub lblLabel1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lblLabel1.MouseUp
        ' Assume no drag operation
        DoDrag = False
    End Sub

    Private Sub lblLabel1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lblLabel1.MouseMove
        ' Perform the drag operation
        If DoDrag Then
            Me.lblLabel1.DoDragDrop("This", DragDropEffects.Move)
        End If
    End Sub

The secret is that it´s possible to detect a double click or drag operation by checking e.clicks parameter in MouseDown event. Thank youthumb up!.

I think it’s not neccesary to set DoDrag variable to false on drop operation. It will work fine setting variable to false just after performing DoDrag method because no other MouseDown event will be raised until user liberates mouse button, so DoDrag variable doesn`t change in all drag&drop operation.

There goes complete code:


' Declares DoDrag variable
Private mbDoDrag As Boolean

...

Private Sub ctlLista_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ctlLista.MouseDown
    ' Detects Drag operation or Double Click (Thanks Drydo)
    If e.Clicks <> 1 Then
        mbDoDrag = False
    Else
        mbDoDrag = True
    End If
End Sub

Private Sub ctlLista_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ctlLista.MouseMove
    ' Begins Drag operation only if DoDrag variable is set
    If e.Button = MouseButtons.Left And mbDoDrag Then
        Dim oDatos As New DatosArrastre("Colaboradores", ctlLista.SelectedItems)

        ctlLista.DoDragDrop(oDatos, DragDropEffects.Move Or DragDropEffects.Copy)
        ' Disables DoDragDrop method until next drag operation
        mbDoDrag = False
    End If
End Sub

And then DoubleClick event will raise correctly and all will work.

Thank you.big grin