Kristian
Kristian

Reputation: 1378

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!

Upvotes: 7

Views: 1805

Answers (1)

Hans Passant
Hans Passant

Reputation: 941565

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

Upvotes: 8

Related Questions