添加拖放后 CellDoubleClick 事件不起作用 [英] CellDoubleClick event doesnt work after addition of Drag&Drop

查看:26
本文介绍了添加拖放后 CellDoubleClick 事件不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我添加了拖动 &拖放到 DataGridView,CellDoubleClick 事件停止工作.在 CellMouseDown 事件中,我有以下代码:

After I added drag & drop to a DataGridView, the CellDoubleClick event stopped working. In the CellMouseDown event I have the following code:

private void dataGridView2_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
    var obj = dataGridView2.CurrentRow.DataBoundItem;
    DoDragDrop(obj, DragDropEffects.Link);
}

如何更正此问题以启用 CellDoubleClick 事件?

How do I correct this to enable CellDoubleClick event?

推荐答案

是的,那行不通.调用 DoDragDrop() 将鼠标控制移交给 Windows D+D 逻辑,这将干扰正常的鼠标处理.您需要延迟启动 D+D,直到您看到用户实际拖动.这应该可以解决问题:

Yes, that cannot work. Calling DoDragDrop() turns mouse control over to the Windows D+D logic, that's going to interfere with normal mouse handling. You need to delay starting the D+D until you see the user actually dragging. This ought to solve the problem:

    Point dragStart;

    private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) {
        if (e.Button == MouseButtons.Left) dragStart = e.Location;
    }

    private void dataGridView1_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e) {
        if (e.Button == MouseButtons.Left) {
            var min = SystemInformation.DoubleClickSize;
            if (Math.Abs(e.X - dragStart.X) >= min.Width ||
                Math.Abs(e.Y - dragStart.Y) >= min.Height) {
                // Call DoDragDrop
                //...
            }
        }
    }

这篇关于添加拖放后 CellDoubleClick 事件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆