Answered Problem with DragDrop and double clicking

jcardana

Old to VB6, New to VB.NET
Joined
Oct 26, 2015
Messages
72
Location
Rio Rancho, NM
Programming Experience
Beginner
There are conflicts between trying to select an item (single-click), double-clicking an item "to open" and dragdrop.
I had the dragdrop working copying one item to another list. But now when I try to doubleclick to "open" an item, I find myself in the drapdrop methods.
As always, I add one thing and I break another.

Thank for any help you have to offer,
Joe

lstFilename (Listbox) Methods:
    Sub lstFilenames_DragDrop(sender As Object, e As DragEventArgs) Handles lstFilenames.DragDrop
        If (e.Data.GetDataPresent(DataFormats.StringFormat)) Then
            Dim rcvFilename As String = CStr(e.Data.GetData(DataFormats.StringFormat))
            Call XferFromComp(rcvFilename)
        End If
    End Sub
   
    Sub lstFilenames_DragOver(sender As Object, e As DragEventArgs) Handles lstFilenames.DragOver
        e.Effect = DragDropEffects.Move
    End Sub
   
    Sub lstFilenames_KeyDown(sender As Object, e As KeyEventArgs) Handles lstFilenames.KeyDown
        If e.KeyCode = Keys.Enter Then
            If SelectedPathFilename <> "" Then OpenPresetFile(SelectedPathFilename)
        End If
    End Sub
   
    Sub lstFilenames_MouseDown(sender As Object, e As MouseEventArgs) Handles lstFilenames.MouseDown
        lstFilenames.SelectedIndex = lstFilenames.SelectedIndex
        Application.DoEvents()
        If lstFilenames.Items.Count = 0 Then
            Return
        Else
            Dim selFilename As String = lstFilenames.Items(lstFilenames.SelectedIndex).ToString
            Dim dde As DragDropEffects
            dde = DoDragDrop(selFilename, DragDropEffects.Move)
        End If
        Application.DoEvents()
        Select Case e.Clicks
            Case 2
                If SelectedPathFilename <> "" Then OpenPresetFile(SelectedPathFilename)
                Application.DoEvents()
        End Select
    End Sub
 
Last edited:
Use MouseDown to store the point, then use MouseMove to calculate a treshold (if MouseButtons.Left) before starting drag-drop . By doing this you allow some slack for drag-drop and it doesn't interfere with click/doubleclick.

Default treshold is 4 pixels, but it could change for system settings and DPI. There is an API for this with SM_CXDRAG setting: GetSystemMetricsForDpi function (winuser.h) - Win32 apps
SM_CXDRAG said:
The number of pixels on either side of a mouse-down point that the mouse pointer can move before a drag operation begins. This allows the user to click and release the mouse button easily without unintentionally starting a drag operation.
 
Thank you... another road block overcome! I just found the reading list so I'm going to try and learn something!
 
Back
Top