我在控件上有一个mousedown事件和click事件。 mousedown事件用于启动拖放操作。我正在使用的控件是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
但是当我点击控件然后只有mousedown事件触发时,click事件不会起火。 如果我在mousedown事件中注释掉“lab.DoDragDrop(lab,DragDropEffects.Copy)”,则click事件会触发。 当我点击控件时,我能做些什么才能使mousedown和click事件发生火灾?
答案 0 :(得分:23)
这是设计的。 MouseDown事件捕获鼠标Control.Capture属性。内置的MouseUp事件处理程序检查鼠标是否仍然被捕获并且鼠标没有移动太远,然后触发Click事件。麻烦的是调用DoDragDrop()将取消鼠标捕获。因为鼠标事件现在用于实现拖放操作,所以必然如此。因此,您永远不会获得Click或DoubleClick事件。
需要响应点击次数和拖放的控件是一个可用性问题。但是,它是可以修复的,您需要做的是确保用户已经从原始鼠标向下移动鼠标,然后开始拖动。使您的代码看起来像这样:
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
答案 1 :(得分:2)
谁需要拖放的c#版本
import re
def find_speech_part(matchobj):
word = matchobj.group(0)
if word.lower() in ["for","and","nor","but","or","yet","so"]:
return "coordinating conjunction"
else:
return word
print(re.sub('\w+', find_speech_part, 'For A but not for B because neither C nor D'))
# coordinating conjunction A coordinating conjunction not coordinating conjunction B because neither C coordinating conjunction D