Relatively Simple Question: MouseDown event handling

I am handling a mousedown event on a button. I want to execute a line of code, say, increase some integer's value by one, as long as the mouse is down on the button.

What i see is, if don't use a loop, the code is executed only once.

The other method i tried was:

1. declared a global variable,

2. execute that one statement in the loop in mousedown event's method as long as the global variable's value doesn't change..

3. change the value of global variable in mouseup ( or  mousereleased ) event.

But the loop in mousedown event never breaks even after the mouse is released.

Any suggestions? I just want to repeatedly do something as long as the mouse is pressed on the button.

Who is Participating?
Arthur_WoodConnect With a Mentor Commented:
try adding a timer,  set the Timer interval to 1000 (One second), then in the MouseDown event, enable the timer.  In the Timer event (which will then go off every second), increment the Integer variable (probably make it at least a Form_Level variable).  Then in the Button's MouseUP event, turn the Timer off again.

Is that what you have in mind?

Arthur Wood
maggariAuthor Commented:
Thanks for your reply.. but timer is the last option i would resort to.. the interval for the timer itself becomes a major issue in my case.. i am working with a socket based application which communicates over a network.. and therefore don't wanna do it with a timer..
is that the only way?
I don't see that you really have an option.

Since everything is event-driven, what you get is events triggering things.  What event do you want to occur when the mouse is down?  If you simply start a counter, how frequently should the counter increment--as fast as the processor or communication can go, or whenever it gets a message (and event!)?

Most likely, you want the counter to occur at certain interval, which means that you need a timer of some sort.
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

clarify ".. the interval for the timer itself becomes a major issue in my case"  what do you mean by that?

Maybe you should clrify exactly what you are trying to accomplish with the Mouse Down, on a Button?
Just off the top of my head....

Dim go As Boolean
Dim MouseCount As Long

Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
go = True 'allow looping to begin
While go
  MouseCount = MouseCount + 1
  DoEvents 'allow messages to be processed
End Sub

Private Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
go = False 'shut down the looping
MsgBox MouseCount
End Sub

Good luck!!
maggariAuthor Commented:
I solved my problem. I used the timer though. Didn't find any alternative. Thank you for your help fellas. Arthur wood gets the points.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.