Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Repeating a procedure several times while holding a button

Status
Not open for further replies.

BostjanPerdan

Programmer
Jan 24, 2006
25
SI
Hi!

How can I achieve that a certain procedure repeats several times when holding a button? That is, if I click the button with a mouse I want the procedure to repeat once but, if I hold the button for a longer time I want it to repeat itself several times depending on the length of the time, I am holding the button.

Cheers,
Bostjan
 
I would maybe work around this concept in a different way.

Check out the KeyDown and KeyPress and KeyUp Events. It looks like the KeyDown might work like you want.
 
This has been driving me up the wall coz I couldn't get it to work at work. Came home and the PC I know has the solution on it won't start up (no power). So here's to reinventing the wheel!!

Code:
Private Sub CommandButton1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
blnButt = True
    Do While blnButt
        OtherProc
        DoEvents
    Loop
End Sub

Private Sub CommandButton1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    blnButt = False
End Sub

Private Sub OtherProc()
    [a1] = [a1] + 1
End Sub

Oh, and as you didn't specify your apliationthis is done in Excel with the CommandButton on a worksheet.

Happy Friday!

;-)
If a man says something and there are no women there to hear him, is he still wrong? [ponder]
How do I get the best answers?
 
Thanks for the reply!

I have copied the code into my computer to test it and it works only partially. When I click the button, the counter starts and continues to count when I remove the mouse pointer away from it.

I would also like to know, if I can include some sort of delay to slow down the repetition rate of the procedure?

Cheers,
Bostjan
 
You need to add blnButt as a PUBLIC level variable:

Code:
[b]Public blnButt As Boolean[/b]
Private Sub CommandButton1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
blnButt = True
    Do While blnButt = True
        OtherProc
        DoEvents
    Loop
End Sub

Private Sub CommandButton1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    blnButt = False
End Sub

Private Sub OtherProc()
    [a1] = [a1] + 1
End Sub

To make the timer go slower, have a look at he Application.WAIT method in the help files - the example is pretty easy to follow - insert that into your loop et voila

Rgds, Geoff

We could learn a lot from crayons. Some are sharp, some are pretty and some are dull. Some have weird names and all are different colours but they all live in the same box.

Please read FAQ222-2244 before you ask a question
 
It works! ;-)

The only remaining problem is, that I want the delay to last a fraction of a second wich it seams impossible to achieve using the Application.Wait method?!

Cheers,
Bostjan
 
For the delay, it isn't an ideal solution but try using a For...Next loop. The effect will obviously change from PC to PC but with a little trial and error you might find a suitable delay. I was using a loop of 1000000 while doing this on my laptop but if I'd been able to start up the old desktop I probably could've painted the house during that sort of loop. Inside AND out!

As for the variable it can be Private if declared in the module (which is what I did and forgot to copy over!) but I'd say that isn't, strictly speaking, good practice!

;-)
If a man says something and there are no women there to hear him, is he still wrong? [ponder]
How do I get the best answers?
 
Loomah, I have just done a similar thing. ;-)

I have simply added a Do While loop which I repeat 1 million times and it does the job for me. Is there a batter, more elegant solution?

Thanks once again to all, who have replied!

Cheers,
Bostjan
 
if App.Wait won't go under 1 second then you may well have to go with your loop

Rgds, Geoff

We could learn a lot from crayons. Some are sharp, some are pretty and some are dull. Some have weird names and all are different colours but they all live in the same box.

Please read FAQ222-2244 before you ask a question
 
BostjanPerdan,

You may also consider use of the Timer function to create the delay. I use the following code to scroll a Grid. When I click the Image controls (Command buttons in your case) and hold the button down the Grid starts scrolling slowly, if I keep the button down it speeds up.

Dim MouseIsDown as Boolean

Private Sub Image1_MouseDown(index As Integer, Button As Integer, Shift As Integer, x As Single, y As Single)

Dim a as string
Dim n as integer
Dim t as single

Grid1.SetFocus
MouseIsDown = True
Select Case index
Case 0
SendKeys "{HOME}", True
Case 1
a$ = "{PGUP}"
GoSub theLoop1
Case 2
a$ = "{UP}"
GoSub theLoop1
Case 3
a$ = "{DOWN}"
GoSub theLoop1
Case 4
a$ = "{PGDN}"
GoSub theLoop1
Case 5
SendKeys "{END}", True
End Select
MouseIsDown = False
Exit Sub

theLoop1:

n = False
Do While MouseIsDown
SendKeys a$, True
DoEvents
If n < 30 Then
n = n + 1
t! = Timer
Do While MouseIsDown And Timer - t! < 0.5 / n: DoEvents: Loop
End If
Loop
Return

End Sub


Private Sub Image1_MouseUp(index As Integer, Button As Integer, Shift As Integer, x As Single, y As Single)

MouseIsDown = False

End Sub

The exact code you require may differ from mine but I hope you get the idea.

Hugh,
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top