为什么在同一个元素火灾mouseDown事件后不DoubleClick事件火?事件、火灾、元素、在同一个

2023-09-04 11:10:04 作者:抹不掉的痛,又有谁会懂

我有一个鼠标按下事件,并点击一个控制事件。鼠标按下事件用于启动的DragDrop操作。我使用的控制是一个目录列表方块。

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

但是,当我点击控制则只有鼠标按下事件触发,单击事件不会起火。 如果我注释掉lab.DoDragDrop(实验室,DragDropEffects.Copy)中的鼠标按下事件,然后单击事件得到火。 我能做些什么使两个鼠标按下,然后单击事件得到火,当我点击控制?

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?

推荐答案

这是设计使然。 MouseDown事件捕获鼠标,Control.Capture财产。内置的MouseUp事件处理程序检查,如果鼠标仍然捕捉和鼠标没有移动过远,则触发Click事件。麻烦的是,在调用的DoDragDrop()将取消鼠标捕获。未必如此,因为鼠标事件现在用来实现拖动+拖放操作。所以,你永远不会得到的点击,也不是DoubleClick事件。

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.

控制,无论需要的点击响应的和的拖动+下降是一个可用性问题。这是可以解决不过,你需要做的是确保用户移动鼠标足够从原来的鼠标按下位置的然后的启动阻力。让你的code是这样的:

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