5

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
  • 113,464
  • 11
  • 89
  • 172
TTGroup
  • 3,575
  • 10
  • 47
  • 79

4 Answers4

6

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
  • 29,157
  • 6
  • 68
  • 77
  • 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
0
public bool down;
private void ControlName_MouseDown(object sender, MouseEventArgs e)
{
    down = true;
}
private void ControlName_EventName(object sender, EventArgs e)
{
    if (down) {// code if the mouse is held
    }
}
private void ControlName_MouseUp(object sender, MouseEventArgs e)
{
    down = false;
}
Kitty
  • 1
  • 1
    Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. **Would you kindly [edit] your answer to include additional details for the benefit of the community?** – Jeremy Caney May 28 '23 at 01:28
0

The accepted answer and comments are already good but the following simple implementation may be useful for posterity:

using System;
using System.Windows.Forms;

namespace WinFormsTest
{
    public partial class Form1 : Form
    {
        private readonly Timer timer;

        public Form1()
        {
            InitializeComponent();
            timer = new Timer();
            timer.Interval = 3000;
            timer.Tick += Timer_Tick;
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            timer.Enabled = false;
            MessageBox.Show("MouseHold");
        }

        private void Button_MouseDown(object sender, MouseEventArgs e)
        {
            timer.Start();
        }

        private void Button_MouseUp(object sender, MouseEventArgs e)
        {
            timer.Enabled = false;
        }
    }
}

Please note that:

  1. This code works for right click also (You may want to work with left click only).
  2. If you complete the MouseUp event outside the Button then this will be ignored and the handler method will work anyway.
Mustafa Özçetin
  • 1,893
  • 1
  • 14
  • 16
-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();           
}
Piotr Siupa
  • 3,929
  • 2
  • 29
  • 65
  • 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