709-218-7927

The Landfall Garden House

60 Canon Bayley Road

Bonavista, Newfoundland

CANADA A0C 1B0

CPRGreaves@gmail.com

Home

Christopher Greaves

Initializing List Boxes

List boxes can be easy to prime, or they can require some programming. In this lesson we are going to take the easy way – priming a list box with a fixed set of province codes.

We would like to initialize the ListBox at the time we initialize the GUI form. To produce a suitable event, double-click on a blank area of the GUI form, just as you would double-click to open up skeletal code for a command button.

Private Sub UserForm_Click()
End Sub

Above is the skeletal code for the event when the user clicks on the GUI form, but we want the skeletal code for the event when the GUI form is activated.

The VBE text cursor still flashes between the “Private” and the “End” of the skeletal procedure. Immediately below the toolbar you will see two drop-down boxes. The first drop-down list shows “UserForm”, and the second shows “Click”. From the second box choose “Initialize”.

Private Sub UserForm_ Initialize ()
End Sub

Then complete the simple chunk of code that will be executed when the GUI form first is loaded.

We consider it good programming practice to clear the ListBox before initializing it, and we have purposely arranged provinces in ascending sequence.

Private Sub UserForm_Initialize()
Me.lbInput.Clear
Me.lbInput.AddItem ("AB")
Me.lbInput.AddItem ("BC")
Me.lbInput.AddItem ("MB")
Me.lbInput.AddItem ("NB")
Me.lbInput.AddItem ("NS")
Me.lbInput.AddItem ("ON")
Me.lbInput.AddItem ("PEI")
Me.lbInput.AddItem ("PQ")
Me.lbInput.AddItem ("SK")
End Sub

When you run your GUI form, you should see the list box populated with province codes, although you can’t really do anything with them yet.

709-218-7927 CPRGreaves@gmail.com

Bonavista, Sunday, December 08, 2024 9:31 AM

Copyright © 1990-2024 Chris Greaves. All Rights Reserved.