Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
665 views
in Technique[技术] by (71.8m points)

.net - Why doesn't doubleclick event fire after mouseDown event on same element fires?

I have a mousedown event and click event on a control. the mousedown event is used for starting dragdrop operation. The control I am using is a Dirlistbox.

 Private Sub Dir1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Dir1.MouseDown

    Dim lab As New Label
    lab.Text = Dir1.DirList(Dir1.DirListIndex)
    lab.DoDragDrop(lab, DragDropEffects.Copy)

End Sub

But when i click on the control then only the mousedown event fires, click event does not get fire. If I comment out "lab.DoDragDrop(lab, DragDropEffects.Copy)" in the mousedown event then click event gets fire. what can I do so that both mousedown and click event gets fire when i click on the control?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is by design. The MouseDown event captures the mouse, Control.Capture property. The built-in MouseUp event handler checks if the mouse is still captured and the mouse hasn't moved too far, then fires the Click event. Trouble is that calling DoDragDrop() will cancel mouse capture. Necessarily so since mouse events are now used to implement the drag+drop operation. So you'll never get the Click nor the DoubleClick event.

Controls that both need to respond to clicks and drag+drop are a usability problem. It is fixable however, what you need to do is ensure that the user has moved the mouse enough from the original mouse down location, then start the drag. Make your code look like this:

Private MouseDownPos As Point

Private Sub Dir1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Dir1.MouseDown
    MouseDownPos = e.Location
End Sub

Private Sub Dir1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Dir1.MouseMove
    If e.Button And MouseButtons.Left = MouseButtons.Left Then
        Dim dx = e.X - MouseDownPos.X
        Dim dy = e.Y - MouseDownPos.Y
        If Math.Abs(dx) >= SystemInformation.DoubleClickSize.Width OrElse _
           Math.Abs(dy) >= SystemInformation.DoubleClickSize.Height Then
            '' Start the drag here
            ''...
        End If
    End If
End Sub

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...