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
Printable View
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
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
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
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
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).
OK, I'm sold. So, Ill use the timer approach.
Thanks, everyone