PDA

View Full Version : [SOLVED] ListBox_Click event fires without clicking



stranno
05-07-2016, 04:31 AM
Hi,
To demonstrate what i mean you open the attached workbook
1) Click on B1
2) Click on B2

Everything works normal.

After that:
Click on the menu button
1) Click on Toggle screen
2) Click on Full screen

Now click on B2. This still works fine.
Than scroll with your mouse untill the red colored cells in Column A touch each other.
Click on B2 and the UserForm disappears immediately. The strange thing is, B1 still works fine.

What is a possible explanation for this behaviour. I spend hours and hours to find out but i did not succeed.

Who knows a way to let it work?

Regards,
Stranno

p45cal
05-07-2016, 03:06 PM
When you click on B2 button the form immediately comes up and the same click seems to be clicking on one of the entries in the list box. If you ensure that the click on B2 is very quick, the form stays open. Solution, bring the form up horizontally away from the vicinity of button B2.
B1 button and its form are already horizontally remote from each other.

stranno
05-07-2016, 03:52 PM
Thanks for your response p45cal. I am going to try tomorrow. It's rather late at the moment. What can possibly trigger this reaction? Is it a bug? In fact i wanted the userforms to popup in the vicinity of the buttons but under the given circomstances that's impossible.

stranno
05-08-2016, 02:57 AM
P45cal, I have tried your workaround and in this case it's maybe the best solution. Ofcourse it's not realy satisfying because the reason why an item is being selected autonomous is still misty. But for now, you've helped me a lot. Thanks.

p45cal
05-08-2016, 03:30 AM
Another solution is to put a delay in, say at the beginning of the Sub Name2 like:
Sub Name2()
Dim EndTime
EndTime = Timer + 0.24
Do
Loop Until Timer > EndTime
If ThisWorkbook.Worksheets("Hulp2").Range("A2") = 0 Then ' PC-scherm

Experiment changing the 0.24 (about a quarter of a second) to something else. It will still self-close if you linger with your finger on the mouse button.

stranno
05-08-2016, 04:57 AM
Oké, I have tried Application. Wait(time) before, but that didn't work. I'll test your second solution as well. But have you any idea why a certain (i don't think it's random) item is being selected?

p45cal
05-08-2016, 05:08 AM
But have you any idea why a certain (i don't think it's random) item is being selected?It's where the mouse pointer is when you click B2 button. On my machine, if the 2 red cells are separated by one cell (instead of being adjacent) a different name is selected because the mouse pointer ends up on a different name. It's certainly not random.

SamT
05-08-2016, 05:41 AM
The MouseUp Event triggers the click on UserForms.


Dim MousedownOnForm As boolean 'UserForm Level Variable

Private Sub UserForm_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
MousedownOnForm = True
End Sub

Private Sub UserForm_Click()
'Apply to any, or all, controls
If not MousedownOnForm then Exit sub
'more code
MousedownOnForm = False
End Sub

stranno
05-08-2016, 06:52 AM
Oh now i understand why a selection takes place. The userform is already installed while at that moment the cursor is positioned somewhere in the listbox. Then vba sees (interprets) it as a mouseclick on that particular point in the listbox.

stranno
05-08-2016, 06:59 AM
And yes Samt, that's a good idea too. I was too much focused on to finding out why it happens instead of thinking of a workaround. thanks guys.