Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 664
Kristian Avatar asked Jan 26 '13 12:01

Kristian


People also ask

How do you trigger Mousedown?

The mousedown event occurs when the left mouse button is pressed down over the selected element. The mousedown() method triggers the mousedown event, or attaches a function to run when a mousedown event occurs. Tip: This method is often used together with the mouseup() method.

How do you trigger a mouse up event?

jQuery mouseup() Method The mouseup event occurs when the left mouse button is released over the selected element. The mouseup() method triggers the mouseup event, or attaches a function to run when a mouseup event occurs. Tip: This method is often used together with the mousedown() method.

Does touch trigger Mousedown?

Because mobile browsers should also work with with web applications that were build for mouse devices, touch devices also fire classic mouse events like mousedown or click . When a user follows a link on a touch device, the following events will be fired in sequence: touchstart. touchend.

Is Mousedown the same as click?

Note: This differs from the click event in that click is fired after a full click action occurs; that is, the mouse button is pressed and released while the pointer remains inside the same element. mousedown is fired the moment the button is initially pressed.


1 Answers

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
                //...
            }
        }
    }
like image 190
Hans Passant Avatar answered Sep 20 '22 10:09

Hans Passant