Consulting

Results 1 to 19 of 19

Thread: Why runs code twice in case of Mousedown event.

  1. #1
    VBAX Tutor
    Joined
    Jun 2005
    Posts
    214
    Location

    Why runs code twice in case of Mousedown event.

    Hi,

    I included an example to demonstrate what i mean.
    Click on the button "Click on me" then a Userform will show up.
    Right Click on a textbox. The message 'hello' appears. Why is the code triggered twice? How can I stop this. I want de message to appear only once.

    Application.EnableEvents = false doesn 't help here.

    I hope one of you can help.
    Attached Files Attached Files

  2. #2
    VBAX Tutor
    Joined
    Jun 2005
    Posts
    214
    Location

    Why runs code twice in case of Mousedown event?


    Hi,

    I included an example to demonstrate what i mean.
    Click on the button "Click on me" then a Userform will show up.
    Right Click on a textbox. The message 'hello' appears. Why is the codetriggered twice? How can I stop this. I want de message to appear only once.
    Application.EnableEvents = false doesn 't help here.
    I hope one of you can help.

    stranno

    Attached Files Attached Files

  3. #3
    VBAX Regular
    Joined
    Nov 2011
    Location
    Ufa
    Posts
    75
    Location
    Try to use MouseUp instead of MouseDown

  4. #4
    VBAX Mentor
    Joined
    Apr 2009
    Location
    Kingsbury
    Posts
    423
    Location
    No Userform or code in your workbook ?

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    There must be something 'special' about right Mouse Down like that

    The event for left MouseDown only runs once.

    Maybe you need to set a flag to avoid the double run

    [VBA]
    Option Explicit
    Dim bAlreadyHaveOne As Boolean
    Private Sub TextBox1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    If Not bAlreadyHaveOne Then
    If Button = 2 Then
    MsgBox "Textbox1 Down"
    bAlreadyHaveOne = True
    End If
    Else
    bAlreadyHaveOne = False
    End If
    End Sub
    Private Sub TextBox2_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    If Not bAlreadyHaveOne Then
    If Button = 2 Then
    MsgBox "Textbox2 Down"
    bAlreadyHaveOne = True
    End If
    Else
    bAlreadyHaveOne = False
    End If
    End Sub
    [/VBA]

    Paul

  6. #6
    VBAX Tutor
    Joined
    Jun 2005
    Posts
    214
    Location

    solved

    Thanks guys,

    Both solutions work well.
    still it are workarounds.
    I wonder why the MouseDown event works as it works.
    Twice firing the event.
    Once again, thanks for the response.

    Stranno

  7. #7
    VBAX Tutor
    Joined
    Jun 2005
    Posts
    214
    Location
    What do you mean Rob342. There is an userform in the workbook

  8. #8
    VBAX Tutor
    Joined
    Jan 2008
    Posts
    262
    Location
    There is no VBA in the uploaded workbook

  9. #9
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Why is the codetriggered twice ?
    Because the event has been triggered twice.
    You will have to invent an equivalent for application.enableevents, because that doesn't work in Userform.

    The most simple event_inteceptor:

    [vba]Private sub Textbox1_change()
    if textbox1.tag=" " then exit sub
    textbox1.tag=" "
    textbox1.Text="different"
    textbox1.tag=""
    End sub[/vba]

    Mutatis mutandum for other events.

  10. #10

  11. #11
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Because the event has been triggered twice.
    Can you expand on that a little? I can see that it's triggered twice for a Right-MouseDown, but as far as I can tell, there's only one 'down' for the right mouse button.

    Does work as expected for a Left MouseDown

    Paul

  12. #12
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    @PH

    I can only register what happens, as you did.
    The event will be triggered twice (cheched using a breakpoint in the code)
    The only guessed 'explanation' I can come up with is that right click has been designed to open (1 click) an options window, that also has to be closed (second) click as well. The closing of the options menu could be considered to be a second mouse event. (???)

  13. #13
    VBAX Expert shrivallabha's Avatar
    Joined
    Jan 2010
    Location
    Mumbai
    Posts
    750
    Location
    If the discussion here is anything to go by then it is a known bug with the MouseDown event. And the "Paul_Hossler workaround" is the only workaround if you are bent on using it!
    Regards,
    --------------------------------------------------------------------------------------------------------
    Shrivallabha
    --------------------------------------------------------------------------------------------------------
    Using Excel 2016 in Home / 2010 in Office
    --------------------------------------------------------------------------------------------------------

  14. #14
    VBAX Tutor
    Joined
    Jun 2005
    Posts
    214
    Location
    "Because the event has been triggered twice"
    I know that. But do you know why the event is triggered twice?

  15. #15
    VBAX Tutor
    Joined
    Jan 2008
    Posts
    262
    Location
    Perhaps you could upload a workbook with the code / userform that you have?

  16. #16
    VBAX Tutor
    Joined
    Jun 2005
    Posts
    214
    Location
    Here 's the workbook (again)
    Attached Files Attached Files

  17. #17
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    "Known bug" I can understand

    I was afraid that it was somehow by design and I was just too dense to figure it out



    Thanks

    Paul

  18. #18
    VBAX Expert shrivallabha's Avatar
    Joined
    Jan 2010
    Location
    Mumbai
    Posts
    750
    Location
    Quote Originally Posted by Paul_Hossler
    "Known bug" I can understand

    I was afraid that it was somehow by design and I was just too dense to figure it out



    Thanks

    Paul
    Initially I worked up the same routine until I re-read your reply. Then my mind started to give me thought bugs like:
    We press the Mouse Down twice [irrespective of Left or Right button] i.e. Once when we right-click and second when we press "OK" button on the msgbox. The second event disregarding "the button we click".

    As an alternative test, I used:
    [VBA]Private Sub TextBox1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    Debug.Print Button
    End Sub[/VBA]
    Here I was sure, there was no second Mouse Down and yet it recorded 2 twice in the immediate window.

    A bit of googling gave me the link I've posted.
    Regards,
    --------------------------------------------------------------------------------------------------------
    Shrivallabha
    --------------------------------------------------------------------------------------------------------
    Using Excel 2016 in Home / 2010 in Office
    --------------------------------------------------------------------------------------------------------

  19. #19
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,059
    Location
    Mutatis mutandum for other events.
    snb, cut to the chase and put this in english, so others can understand.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Posting Permissions

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