PDA

View Full Version : Why runs code twice in case of Mousedown event.



stranno
07-06-2012, 03:44 AM
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.

stranno
07-06-2012, 04:06 AM
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

nilem
07-06-2012, 05:03 AM
Try to use MouseUp instead of MouseDown

Rob342
07-06-2012, 05:15 AM
No Userform or code in your workbook ?

Paul_Hossler
07-06-2012, 07:03 AM
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


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


Paul

stranno
07-07-2012, 01:27 AM
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

stranno
07-07-2012, 04:56 AM
What do you mean Rob342. There is an userform in the workbook

sassora
07-07-2012, 05:29 AM
There is no VBA in the uploaded workbook

snb
07-07-2012, 05:50 AM
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:

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

Mutatis mutandum for other events.

snb
07-07-2012, 06:00 AM
see http://vbaexpress.com/forum/showpost.php?p=271810&postcount=5

Paul_Hossler
07-07-2012, 06:51 AM
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

snb
07-07-2012, 07:43 AM
@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. (???)

shrivallabha
07-07-2012, 09:43 AM
If the discussion here (http://www.mrexcel.com/forum/showthread.php?475413-MouseDown-event-constants&p=2348222&viewfull=1#post2348222) 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!

stranno
07-07-2012, 11:07 AM
"Because the event has been triggered twice"
I know that. But do you know why the event is triggered twice?

sassora
07-07-2012, 11:14 AM
Perhaps you could upload a workbook with the code / userform that you have?

stranno
07-07-2012, 11:26 AM
Here 's the workbook (again)

Paul_Hossler
07-07-2012, 02:05 PM
"Known bug" I can understand

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

:think:

Thanks

Paul

shrivallabha
07-07-2012, 09:40 PM
"Known bug" I can understand

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

:think:

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:
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
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.

Aussiebear
07-08-2012, 12:52 AM
Mutatis mutandum for other events.

snb, cut to the chase and put this in english, so others can understand.