开发者

CellDoubleClick event doesnt work after addition of Drag&Drop

开发者 https://www.devze.com 2023-01-04 16:33 出处:网络
After I added drag & drop to a DataGridView, the CellDoubleClick event stopped working. In the CellMouseDown event I have the following code:

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);
}

How do开发者_高级运维 I correct this to enable CellDoubleClick event?


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
                //...
            }
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号