C# distinguish Drag drop and Mouse click

感情迁移 提交于 2019-12-12 05:19:55

问题


I have a form which allows drop and also I need to capture the click event.

The tricky part here, is when the user drags and drop the object on the form, the MouseClickEvent also fired-because he also clicked the mouse to release the drop.

How may I handle each on of this events in a separate event trigger?

Thanks in advance!

This is a short and more logic question so i didn't see a real reason to put pieces of code here..

Thanks!


回答1:


Add a global boolean, for instance:

private bool isDragAndDrop;

Set it to false when loading the form. When the dragAndDrop event is fired you should set isDragAndDrop = true.

When the Click event is fired you check if(!isDragAndDrop) This will or will not execute the code inside the click event based on the value on the isDragAndDrop -variable.

Before leaving the click event you the set isDragAndDrop = false




回答2:


I found this solution on this link

    private void MyMouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left && e.Clicks == 1)
        {
            Control source = (Control)sender;
            source.DoDragDrop(new MyWrapper(source), DragDropEffects.Move);
        }
    }

by Bill Rawlinson — 18 Apr 2007



来源:https://stackoverflow.com/questions/32384222/c-sharp-distinguish-drag-drop-and-mouse-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!