Please Help: Userform acting like I'm double clicking

Peter h

Active Member
Joined
Dec 8, 2015
Messages
417
I have a userform with a listbox. When a selection is made from this listbox, controls become visible depending on the selection made, and this listbox becomes invisible. When I make 1 certain selection the alignment of another listbox that becomes visible is right where the original listbox is, so when I make that selection it automatically selects something in the second listbox when it becomes visible. I know it's not the mouse, because this is happening on different computers. I've also tried putting an Application.Wait for 2 seconds on the listbox1 click event, and it still makes the selection in the second listbox. My solution to this was just move the second listbox so that it is no longer aligned with the original listbox. Does anyone have any ideas why this would be happening?
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Please post the userform code - identify the bit that is causing the problem
In your reply, click on # icon (above post window) first and then and paste code BETWEEN the code tags (they appear automatically after clicking on the icon)

thanks
 
Upvote 0
How about something like this :
Code:
    Dim t As Single     
    ListBox1.Visible = False
    With ListBox2
        .Locked = True
        .Visible = False
        t = Timer
        Do
            DoEvents
        Loop Until Timer - t >= 0.1
        .Visible = True
        .Locked = False
    End With
 
Upvote 0
Or for a more robust and screen-flicker free code use this :
Code:
Option Explicit

[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If"]#If[/URL]  VBA7 Then
    Private Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, lParam As Any) As LongPtr
    Private Declare PtrSafe Function WindowFromAccessibleObject Lib "oleacc" (ByVal pacc As IAccessible, phwnd As LongPtr) As Long
    Private hwnd As LongPtr
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else"]#Else[/URL] 
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Private Declare Function WindowFromAccessibleObject Lib "oleacc" (ByVal pacc As IAccessible, phwnd As Long) As Long
    Private hwnd As Long
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End"]#End[/URL]  If

Private Const WM_SETREDRAW = &HB

Private Sub ListBox2_Change()
    Dim sngTop As Single, sngLeft As Single
    Dim sngTimer As Single

    On Error GoTo Xit:

    sngTop = Me.ListBox2.Top
    sngLeft = Me.ListBox2.Left
    ListBox1.Top = sngTop
    ListBox1.Left = sngLeft
 
    WindowFromAccessibleObject Me, hwnd

    SendMessage hwnd, ByVal WM_SETREDRAW, ByVal 0&, 0&
     
    ListBox2.Visible = False
    With ListBox1
        .Locked = True
        .Visible = False
        sngTimer = Timer
        Do
            DoEvents
        Loop Until Timer - sngTimer >= 0.1
        .Visible = True
        .Locked = False
    End With
Xit:
    SendMessage hwnd, ByVal WM_SETREDRAW, ByVal 1&, 0&
    Me.Repaint
End Sub

Where ListBox2 is the listbox that becomes invisible and ListBox1 the one that appears right where the original Listbox2 was.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top