|
-
Jul 25th, 2011, 01:41 PM
#1
[RESOLVED] How to make a continueous button
I need a button that when I hold it down it will cycle through the loop repeatetly until I release it.
Code:
Private Sub cmdMovePanelLeft()
'
' <--- what to add here
'
picPanel.Left = picPanel.Left + 1
'
' <-- what to add here
'
End Sub
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
Jul 25th, 2011, 02:12 PM
#2
Re: How to make a continueous button
One method is to use a timer.
1. In the button's MouseDown event, test for Left button, and if so, set timer interval and enable timer
2. In the button's MouseUp event, test for Left button, and if so, disable timer
3. In the timer event, move your panel by 1 pixel or twip as appropriate for scalemode used
Edited: Probably should perform similar panel moves during a button's click event too since those events are fired continuously while button has focus and Enter key is held down. If you do handle the click event, know that the mouse button being released can fire a click event too. In this case, you probably don't want to move the panel 1 extra time due to a click event occurring (the timer has been moving it while the button was down). I'd probably handle the situation by using a form-level boolean value (or the Tag property of the button). In the MouseDown event set that boolean/Tag value. In the click event, test if the boolean/Tag is set & if so, don't move the panel. If not set, then user clicked button with keyboard instead of mouse. In the MouseUp event reset that boolean/Tag value. Just an idea.
^^ Better yet. Just test to see if the Timer is enabled vs. using some extraneous value
Last edited by LaVolpe; Jul 25th, 2011 at 02:53 PM.
-
Jul 25th, 2011, 10:07 PM
#3
Re: How to make a continueous button
jms, as lavolpe said, something like this, this just add / subtract the value while pressing button, just add 2 cmd button, 1 label, 1 timer
Code:
Dim Cnt As Integer, Cmd As Integer
Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
Cmd = 1
Timer1.Enabled = True
End If
End Sub
Private Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
Timer1.Enabled = False
End If
End Sub
Private Sub Command2_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
Cmd = 2
Timer1.Enabled = True
End If
End Sub
Private Sub Command2_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
Timer1.Enabled = False
End If
End Sub
Private Sub Form_Load()
Timer1.Interval = 10
Timer1.Enabled = False
End Sub
Private Sub Timer1_Timer()
If Cmd = 1 Then
Cnt = Cnt - 1
ElseIf Cmd = 2 Then
Cnt = Cnt + 1
End If
Label1.Caption = Cnt
End Sub
-
Jul 26th, 2011, 01:41 PM
#4
Re: How to make a continueous button
I didn't see a purpose in using a timer. What could that do that the below can't?:
Code:
Dim KeepDoingItAs Boolean
Private Sub cmdMovePanelLeft_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbLeftButton Then
KeepDoingIt = True
Do While KeepDoingIt
DoEvents
picPanel.Left = picPanel.Left + 1
Loop
End If
End Sub
Private Sub cmdMovePanelLeft_MouseUp(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
KeepDoingIt = False
End Sub
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
Jul 26th, 2011, 01:58 PM
#5
Re: How to make a continueous button
A timer would keep the speed constant and controllable.
Without it the speed can vary wildly, and can be far too fast depending on circumstances (such as CPU speed, and other programs running, etc).
-
Jul 26th, 2011, 02:35 PM
#6
Re: How to make a continueous button
OK, I'm sold. So, Ill use the timer approach.
Thanks, everyone
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|