4

I want to catch the event that user clicks and holds mouse on a control in C#.

I have read on MSDN and I only see events Mouse Down, Mouse Up, ... but don't have Move Hold event.

Enigmativity
  • 105,241
  • 11
  • 83
  • 163
TTGroup
  • 3,417
  • 10
  • 42
  • 76

2 Answers2

5

You need to use mentinoed events with some timer between them.

Example:

  • MouseDown
    • Start Timer
  • MouseUp
    • Disable Timer

In case if user holds more then timer time - invoke your event handler, when mouseUp happend faster then timer elapsed - disable runned timer.

Samich
  • 28,257
  • 5
  • 65
  • 76
  • Thank you, Using timer is ok ^.^ – TTGroup Sep 14 '12 at 14:44
  • I didnt get the idea, would you explain it please? – Arif YILMAZ Aug 05 '13 at 08:14
  • You need to create `Timer` object and subscribe for events mentioned in the answer. When `MouseDown` event fires - you need to start the timer, when `MouseUp` fires and timer still in process - do your action, otherwise - release your timer. – Samich Aug 07 '13 at 06:08
-1

First, you should use stop watch to detect time you want.

using System.Diagnostics;

Second, define a global instance of stopwatch class.

Stopwatch s = new Stopwatch();

This is the first event you should use:

private void controlName_MouseDown(object sender, MouseEventArgs e)
{
    s.Start();
}

This is the second event you should use:

private void controlName_MouseUp(object sender, MouseEventArgs e)
{
    s.Stop();
    //determine time you want . but take attention it's in millisecond
    if (s.ElapsedMilliseconds <= 700 && s.ElapsedMilliseconds >= 200)
    {
        //you code here.
    }           
    s.Reset();           
}
NO_NAME
  • 2,769
  • 1
  • 23
  • 56
  • This solution isn't any good for creating a mouse hold event. This just works out if the user held down the mouse for a period of time but only after they release the mouse. It's too late then. And what if the user holds the mouse down for an hour? Nothing happens. It's a poor user experience. – Enigmativity Aug 31 '15 at 00:22
  • that's depends on you what you want to do exactly .. if you don't want to determine a particular time you can change if statement or delete it .. – Beshoy Nabeih Aug 31 '15 at 18:22