Forum Discussion

S_Leonardo's avatar
S_Leonardo
Occasional Contributor
9 years ago

Problem showing a listbox with selected items using .ShowModal

Hi,

I got some difficulties trying to load a listbox with some selected items using .ShowModal

 

Example of my code(VBScript):

 

With UserForms.MyForm.ListBoxABC

  .Add("Test_01")

  .Add("Test_02")

  .Add("Test_03")

End With

'

UserForms.MyForm.ListBoxABC.Selected( 0) = True 'This select the fist line of the listbox (Test_01)

UserForms.MyForm.ShowModal 

 

When i call .ShowModal it loads the ListBoxABC without any item selected.

Using .Show it works, i got the expected result (Test_01 is selected).

 

Anyone have any ideas?

Thanks.

  • Ryan_Moran's avatar
    Ryan_Moran
    9 years ago

    The form is not yet shown when you are setting the selected property of the item and therefore cannot be set.

    You need to add an event handler for the OnShow event of the userform and set the selected property after the form is shown.

     

    With UserForms.MyForm.ListBoxABC
      .Add("Test_01")
      .Add("Test_02")
      .Add("Test_03")
    End With
    '
    
    UserForms.MyForm.ShowModal 
    
    Sub OnShow_Handler(Sender)
        'Set this handler under the events section for the form in the designer
        UserForms.MyForm.ListBoxABC.Selected( 0) = True
    End Sub

    Reference event handling here.

     

    • Ryan_Moran's avatar
      Ryan_Moran
      Valued Contributor

      The form is not yet shown when you are setting the selected property of the item and therefore cannot be set.

      You need to add an event handler for the OnShow event of the userform and set the selected property after the form is shown.

       

      With UserForms.MyForm.ListBoxABC
        .Add("Test_01")
        .Add("Test_02")
        .Add("Test_03")
      End With
      '
      
      UserForms.MyForm.ShowModal 
      
      Sub OnShow_Handler(Sender)
          'Set this handler under the events section for the form in the designer
          UserForms.MyForm.ListBoxABC.Selected( 0) = True
      End Sub

      Reference event handling here.