excelvbalistboxuserform

After loading a userform, the first click on a listbox does not select the clicked item


After loading a userform, the first click on a listbox does not always result in the clicked item being selected. It seems to take 1 click to first wake-up the listbox and a 2nd click to start selecting items as normal. This can also affect additional listboxes on the same userform.

I want the first click on a listbox to always register and result in the clicked item being selected.


Solution

  • In the Userform Initialization event, looping through all controls on a userform to run the SetFocus event forces all controls to also complete initialization, "waking" them up so that the first click by a user on any listbox will always register and the clicked item will be selected.

    Private Sub UserForm_Initialize()
    
        Dim c As Control      
    
        For Each c In Me.Controls
            c.SetFocus
        Next c
    
    End Sub