DataGridView CellDrag太敏感了

时间:2013-02-09 11:44:25

标签: c# datagridview drag-and-drop

我在winforms上有一个datagridview控件,并使用以下事件将'row'拖出datagrid:

    private void gridOperations_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
    {
        if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
        {

            if (Math.Abs(e.X - mouseDownPos.X) >= SystemInformation.DoubleClickSize.Width || Math.Abs(e.Y - mouseDownPos.Y) >= SystemInformation.DoubleClickSize.Height)
            {
                string[] filesToDrag = { "tmp/generated.log" };
                gridOperations.DoDragDrop(new DataObject(DataFormats.FileDrop, filesToDrag), DragDropEffects.Copy);
            }
        }
    }

问题是我在datagridview上也有点击和双击事件,双击事件几乎从不会被执行,除非我在点击时根本不移动我的鼠标。如何添加'thershold'以便如果我按住鼠标并将单元格拖动3个像素,那么它将触发gridOperations.DoDragDrop?谢谢!

1 个答案:

答案 0 :(得分:1)

尝试使用整数计数器。 每次触发事件时都会增加int,如果达到阈值,则执行代码的其余部分并将其重置为0.

像:

private int thCount = 0;
    private void gridOperations_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
    {
        if ((e.Button & MouseButtons.Left) == MouseButtons.Left && thCount==5)
        {
            //... 
            thCount = 0;
        }
        else
        {
            thCount++;
        }
    }
希望我能帮助你