Basically let's say I'm making a simple counter, when I click the mouse, the counter goes up. My question is how do I program an even that makes it possible for me to have the counter flowing while the mouse I held down, basically something that works exactly as the KeyPress event handler, only with a mouse.
-
2I don't think the MouseDown event will fire continuously, so use it in conjunction with a Timer to do the counting .. make sure to *not* block any events or the UI will stop being responsive (please no suggestions for DoEvents here!). That is, there should be *no* do/while loops in this approach. – Oct 27 '12 at 20:56
-
[MSDN Link](http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousebuttons(v=vs.100).aspx) should work. – matthewr Oct 27 '12 at 21:13
3 Answers
If you don't need a timer running for some other reason, I wouldn't use a timer. I would use a class property or global variable, depending on what you're doing.
private/public startTicks long = 0;
When you fire the MouseDown event:
startTicks = DateTime.Now.Ticks;
On the MouseUp event, take the difference and convert it to whatever time element (min, sec, hour) you want.
DoConversion(DateTime.Now.Ticks - startTicks);

- 13,361
- 16
- 48
- 78
-
Yatrix, thanks that got me started, however I also want the label to show the counter going up as the mouse button is pushed down, that's how it works for KeyPressed. Any idea for that? – ab1428x Oct 28 '12 at 13:40
-
For that, you will probably need a timer to update it in defined intervals. – Yatrix Oct 28 '12 at 18:17
-
@AndrewB If I answered your question, please upvote and accept the answer. Glad I could help. – Yatrix Oct 29 '12 at 00:21
I'm going to suggest a solution based on using Microsoft's Reactive Framework (Rx).
I've assumed that the MouseDown
& MouseUp
events are based on clicking the form and that there is a NumericUpDown
control that we wish to increment every 0.1 seconds while the mouse is down.
Here's the Reactive Framework way of coding the solution:
var mouseDowns = Observable.FromEventPattern
<MouseEventHandler, MouseEventArgs>(
h => this.MouseDown += h,
h => this.MouseDown -= h);
var mouseUps = Observable.FromEventPattern
<MouseEventHandler, MouseEventArgs>(
h => this.MouseUp += h,
h => this.MouseUp -= h);
var intervals = Observable.Interval(TimeSpan.FromSeconds(0.1));
var query =
from md in mouseDowns
select intervals.TakeUntil(mouseUps);
query.Switch().ObserveOn(this).Subscribe(n => numericUpDown1.Value += 1);
The reactive query should be very easy to understand its purpose - basically it is "When you get a mouse down select the intervals until there is a mouse up."
The type of query
is IObservable<IObservable<long>>
so before the Subscribe
we need to call Switch
to turn the query into an IObservable<long>
.
The ObserveOn(this)
makes sure that the values of the observable are marshalled to the UI thread.
Rx can be a little tricky to learn, but once you've got it it is very powerful. I use it all the time.

- 113,464
- 11
- 89
- 172
Define a timer that starts firing its events when the MouseDown
event occurs. Then set the MouseUp
event to pause or stop the timer. Execute the code you want to be triggered when the timer elapses.

- 2,415
- 1
- 18
- 31