dcsimg
Results 1 to 9 of 9

Thread: [RESOLVED] Auto Repeat Command Button

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2018
    Posts
    195

    Resolved [RESOLVED] Auto Repeat Command Button

    Below is my code to auto repeat a Command Button

    Code:
    Option Explicit
    
    Dim myCounter As Long
    
    Private Sub Command1_Click()
        myCounter = myCounter + 1
        Label1.Caption = "Command1 executed " & myCounter & " times."
    End Sub
    
    Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
        Timer1.Interval = 1
        Timer1.Enabled = True
    End Sub
    
    Private Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
        myCounter = 0
        Timer1.Interval = 0
        Timer1.Enabled = False
    End Sub
    
    Private Sub Timer1_Timer()
        Call Command1_Click
    End Sub
    It works OK, but what I really want is to wait a specified time before the auto repeat actually executes (say 1/2 second)
    Let's say the user pressed the button, and did not hold down for the 1/2 second, the command button should only execute ONCE.
    (ie if there was a click (but no real HOLD DOWN of the button)

    I cannot figure this out.
    Any help would be appreciated.

    Changing the Timer interval to 500 (1/2 second) sort of works, but then the Auto Repeat runs way to slow.

  2. #2
    Fanatic Member
    Join Date
    Nov 2017
    Posts
    859

    Re: Auto Repeat Command Button

    Not how I would probably normally do it myself, but building off of your code:

    Code:
    Option Explicit
    
    Dim myCounter As Long
    Dim blnInit As Boolean
    
    Private Sub Command1_Click()
        myCounter = myCounter + 1
        Label1.Caption = "Command1 executed " & myCounter & " times."
    End Sub
    
    Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
        Timer1.Interval = 500
        blnInit = True
        Timer1.Enabled = True
    End Sub
    
    Private Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
        myCounter = 0
        Timer1.Interval = 0
        Timer1.Enabled = False
    End Sub
    
    Private Sub Timer1_Timer()
        If blnInit = True Then
          blnInit = False
          Timer1.Interval = 1
        Else
          Call Command1_Click
        End If
    End Sub

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2018
    Posts
    195

    Re: Auto Repeat Command Button

    Works perfect!

    Thank you

    Just curios... how would you do it?

  4. #4
    Fanatic Member
    Join Date
    Nov 2017
    Posts
    859

    Re: Auto Repeat Command Button

    For sure I would put code that does work in the Timer() event itself rather than calling the button's click event.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Nov 2018
    Posts
    195

    Re: Auto Repeat Command Button

    OK

    Not sure what you mean, but thanks.

    Also, as I study your solution I cannot figure out what it is actually doing that gives me my desired result.
    (I must be really having a bad day)

  6. #6
    Hyperactive Member
    Join Date
    Aug 2017
    Posts
    288

    Re: Auto Repeat Command Button

    Another solution:

    Code:
    Option Explicit
    
    Private myCounter As Long
    
    Private Sub Command1_Click()
        myCounter = myCounter + 1&
        Label1.Caption = "Command1 executed " & myCounter & " times."
    End Sub
    
    Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
        Timer1.Interval = 500&
        Timer1.Enabled = True
    End Sub
    
    Private Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
        myCounter = 0&
        Timer1.Enabled = False
    End Sub
    
    Private Sub Timer1_Timer()
        If Timer1.Interval = 500& Then Timer1.Interval = 1&
        Command1_Click
    End Sub
    Last edited by Victor Bravo VI; Nov 8th, 2019 at 06:08 PM.

  7. #7
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    5,842

    Re: Auto Repeat Command Button

    Here's a cutdown version of how I do it:

    Code:
    
    Option Explicit
    '
    Private Enum AlterConst
        NoAlter = 0
        LargerAlter = 1
        SmallerAlter = 2
        UpAlter = 3
        DownAlter = 4
        LeftAlter = 5
        RightAlter = 6
        RotateLeftAlter = 7
        RotateRightAlter = 8
    End Enum
    Dim Alter As AlterConst
    '
    Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
    '
    
    Private Sub cmdLarger_MouseDown(Button As Integer, Shift As Integer, x As Single, Y As Single)
        MakeLarger
        Alter = LargerAlter
        Timer1.Interval = 500
        Timer1.Enabled = True
    End Sub
    
    Private Sub cmdLarger_MouseUp(Button As Integer, Shift As Integer, x As Single, Y As Single)
        Alter = NoAlter
    End Sub
    
    Private Sub Timer1_Timer()
        Const VK_LBUTTON = 1
        If GetKeyState(VK_LBUTTON) >= 0 Or Alter = NoAlter Then
            Alter = NoAlter
            Timer1.Enabled = False
            Exit Sub
        End If
        Select Case Alter
        Case LargerAlter:       MakeLarger
        Case SmallerAlter:      MakeSmaller
        Case UpAlter:           MoveUp
        Case DownAlter:         MoveDown
        End Select
        Timer1.Interval = 10
    End Sub
    
    Sub MakeSmaller()
        '
    End Sub
    
    Sub MakeLarger()
        '
    End Sub
    
    Sub MoveUp()
        '
    End Sub
    
    Sub MoveDown()
        '
    End Sub
    
    
    Obviously, I've only got one Mouse_Down and Mouse_Up event. And I've got no code in the procedures that actually do stuff. But it should provide you with a good template.

    EDIT1: Also, I cut out some of the functionality in my Timer1 event (the Select Case), but didn't cut it out of the Enum. But again, it gives you the idea. I use the same timer to monitor several buttons.

    EDIT2: Also, on a rare moon, VB6 would miss the Mouse_Up event. That's why the GetKeyState API call is in there. That makes sure we stop when the mouse is up.

    EDIT3: That code is really old (but it does what it's suppose to). However, if I were re-writing it, I might clean it up a bit. For one, I might check the Button pushed in the Mouse_Down event and Mouse_Up event. Also, just little things like declaring that VK_LBUTTON constant as Long.
    Last edited by Elroy; Nov 8th, 2019 at 06:42 PM.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  8. #8
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    5,842

    Re: Auto Repeat Command Button

    There, cleaned up just a bit:

    Code:
    
    Option Explicit
    '
    Private Enum AlterConst
        NoAlter = 0&
        LargerAlter = 1&
        SmallerAlter = 2&
        UpAlter = 3&
        DownAlter = 4&
    End Enum
    Dim Alter As AlterConst
    '
    Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
    '
    Private Const VK_LBUTTON As Long = 1&
    '
    
    Private Sub cmdLarger_MouseDown(Button As Integer, Shift As Integer, x As Single, Y As Single)
        If Button = VK_LBUTTON Then
            MakeLarger
            Alter = LargerAlter
            Timer1.Interval = 500&
            Timer1.Enabled = True
        End If
    End Sub
    
    Private Sub cmdLarger_MouseUp(Button As Integer, Shift As Integer, x As Single, Y As Single)
        If Button = VK_LBUTTON Then
            Alter = NoAlter
        End If
    End Sub
    
    Private Sub Timer1_Timer()
        Const VK_LBUTTON As Long = 1&
        If GetKeyState(VK_LBUTTON) >= 0 Or Alter = NoAlter Then
            Alter = NoAlter
            Timer1.Enabled = False
            Exit Sub
        End If
        Select Case Alter
        Case LargerAlter:       MakeLarger
        Case SmallerAlter:      MakeSmaller
        Case UpAlter:           MoveUp
        Case DownAlter:         MoveDown
        End Select
        Timer1.Interval = 10&
    End Sub
    
    Sub MakeSmaller()
        '
    End Sub
    
    Sub MakeLarger()
        '
    End Sub
    
    Sub MoveUp()
        '
    End Sub
    
    Sub MoveDown()
        '
    End Sub
    
    
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Nov 2018
    Posts
    195

    Re: Auto Repeat Command Button

    I've been away for a while, but I'm back.

    Thank you also Victor Bravo VI and Elroy
    Two more solutions.

    Victor Bravo VI's is so simple

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width