none
How to check if the mousedown on a button has been released? RRS feed

  • Question

  • I am trying to program a value in a 'txtpercent' text box to increment while an 'cmdIncrease' button is pressed with the left mousebutton.

    I have used the mousedown event for the button to call a public 'doIncrement' function in a module:

    Private Sub cmdIncrease_MouseDown(button As Integer, Shift As Integer, X As Single, Y As Single)
        IncMouseIsDown = True
            Call doIncrement
    End Sub

    Then when the mouseup event for the button is triggered a IncMouseIsDown = false is set:

    Private Sub cmdIncrease_MouseUp(button As Integer, Shift As Integer, X As Single, Y As Single)
        IncMouseIsDown = False
    End Sub

    The 'doIncrement' function needs to continue increasing the 'txtpercent' value until the 'cmdIncrease' button is released.

    However, the 'doIncrement' function never breaks out of the loop, and doesn't notice when the left mousebutton on the 'cmdIncrease' button is released. 

    Public IncMouseIsDown As Boolean
    Public IncMouseIsUp As Boolean

    Public Sub doIncrement()
        Do
            If button = acLeftButton Then
                txtPercent = txtPercent + 1
            End If
        Loop While IncMouseIsDown
    End Sub

    Anyone have any ideas how I can get the do while loop to repeatedly check if the left mouse button on the 'cmdIncrease' button is still pressed?

    Your help is much appreciated.

    Thank you

    Nath

    Monday, December 5, 2016 8:45 PM

Answers

  • The 'doIncrement' function needs to continue increasing the 'txtpercent' value until the 'cmdIncrease' button is released.

    Hi Nath,

    I do not know how to achieve this with a MouseDown event, but you could use a CommandButton, with its property AutoRepeat set to True.

    Imb.

    • Marked as answer by NaPazz Monday, December 5, 2016 9:45 PM
    Monday, December 5, 2016 8:53 PM
  • However, I would like to see the value changing while the buttons pressed. 

    With autorepeat, releasing the button suddenly shows a huge jump in the value.

    Hi Nath,

    You can use the Click event of the CommandButton to increase the percent value by 1.

    In combination with the AutoRepeat property of the CommandButton you would see a continuous changing of the percent value, as long as the button is pressed.

    Imb.


    • Edited by Imb-hb Monday, December 5, 2016 9:35 PM edit: as long ...
    • Marked as answer by NaPazz Monday, December 5, 2016 9:42 PM
    Monday, December 5, 2016 9:34 PM
  • Thank you Imb.

    I needed to call the following 'Pause' function with an interval of 0.05 in the click event of the command button.

    Now it works perfectly. I can now have a decent spin button on a continuous form, which wasn't possible with the spin button active x version.

    Cheers.

    Public Function Pause(NumberOfSeconds As Variant)
        On Error GoTo Error_GoTo

        Dim PauseTime As Variant
        Dim Start As Variant
        Dim Elapsed As Variant

        PauseTime = NumberOfSeconds
        Start = Timer
        Elapsed = 0
        Do While Timer < Start + PauseTime
            Elapsed = Elapsed + 1
            If Timer = 0 Then
                ' Crossing midnight
                PauseTime = PauseTime - Elapsed
                Start = 0
                Elapsed = 0
            End If
            DoEvents
        Loop

    Exit_GoTo:
        On Error GoTo 0
        Exit Function
    Error_GoTo:
        Debug.Print Err.Number, Err.Description, Erl
        GoTo Exit_GoTo
    End Function


    Nath

    • Marked as answer by NaPazz Monday, December 5, 2016 9:45 PM
    Monday, December 5, 2016 9:45 PM

All replies

  • The 'doIncrement' function needs to continue increasing the 'txtpercent' value until the 'cmdIncrease' button is released.

    Hi Nath,

    I do not know how to achieve this with a MouseDown event, but you could use a CommandButton, with its property AutoRepeat set to True.

    Imb.

    • Marked as answer by NaPazz Monday, December 5, 2016 9:45 PM
    Monday, December 5, 2016 8:53 PM
  • Monday, December 5, 2016 8:56 PM
  • Thanks Imb.

    I think the autorepeat is the way to go. It increments while the button is pressed.

    However, I would like to see the value changing while the buttons pressed. 

    With autorepeat, releasing the button suddenly shows a huge jump in the value.

    Any ideas how I can get the txtPercent box to reflect the increasing autorepeat value?

    Ram,

    Trying the following produced a run-time error '424' Object required:

    Public Sub doIncrement(button As Integer)
        
        Do
            If cmdIncrease.ButtonState = MouseButtonState.Pressed Then
                txtPercent = txtPercent + 1
                incmousedown = True
                ' If the button is released
            ElseIf cmdIncrease.ButtonState = MouseButtonState.Released Then
                incmousedown = False
            End If
            
        Loop While IncMouseIsDown

    End Sub

    Any suggestions?

    Thanks


    Nath

    Monday, December 5, 2016 9:27 PM
  • However, I would like to see the value changing while the buttons pressed. 

    With autorepeat, releasing the button suddenly shows a huge jump in the value.

    Hi Nath,

    You can use the Click event of the CommandButton to increase the percent value by 1.

    In combination with the AutoRepeat property of the CommandButton you would see a continuous changing of the percent value, as long as the button is pressed.

    Imb.


    • Edited by Imb-hb Monday, December 5, 2016 9:35 PM edit: as long ...
    • Marked as answer by NaPazz Monday, December 5, 2016 9:42 PM
    Monday, December 5, 2016 9:34 PM
  • Thank you Imb.

    I needed to call the following 'Pause' function with an interval of 0.05 in the click event of the command button.

    Now it works perfectly. I can now have a decent spin button on a continuous form, which wasn't possible with the spin button active x version.

    Cheers.

    Public Function Pause(NumberOfSeconds As Variant)
        On Error GoTo Error_GoTo

        Dim PauseTime As Variant
        Dim Start As Variant
        Dim Elapsed As Variant

        PauseTime = NumberOfSeconds
        Start = Timer
        Elapsed = 0
        Do While Timer < Start + PauseTime
            Elapsed = Elapsed + 1
            If Timer = 0 Then
                ' Crossing midnight
                PauseTime = PauseTime - Elapsed
                Start = 0
                Elapsed = 0
            End If
            DoEvents
        Loop

    Exit_GoTo:
        On Error GoTo 0
        Exit Function
    Error_GoTo:
        Debug.Print Err.Number, Err.Description, Erl
        GoTo Exit_GoTo
    End Function


    Nath

    • Marked as answer by NaPazz Monday, December 5, 2016 9:45 PM
    Monday, December 5, 2016 9:45 PM