简体   繁体   中英

make delayed mousedown event

I have a datagridview which cells has a click event. The cells also have the following mouseDown event:

if (e.Button == MouseButtons.Left && e.Clicks == 1)
{
    string[] filesToDrag = 
    {
        "c:/install.log"
    };

    gridOperations.DoDragDrop(new DataObject(DataFormats.FileDrop, filesToDrag), DragDropEffects.Copy);
}

whenever I try to click a cell, the mousedown event instantly fires and tries to drag the cell. How can I make the mousedown event fire only if user has holded mouse down for 1 second for example? Thanks!

The proper way to do this is not by time but to trigger it when the user moved the mouse enough. The universal measure for "moved enough" in Windows is the double-click size. Implement the CellMouseDown/Move event handlers, similar to this:

    private Point mouseDownPos;

    private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) {
        mouseDownPos = e.Location;
    }

    private void dataGridView1_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) {
                // Start dragging
                //...
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM