A Little Bit About Combo Boxes
Some Background. In order to work the homework you are likely to need just a little more background about combo boxes. Combo boxes are useful when you want to limit the selections the user can make to a particular list of items. This can also be done with the list box, but the combo box has the advantage of usually requiring less space on the form.
Some of the first questions that might come to mind when working with a combo box are
There are a variety of ways to do the answer the first question. I only know of one way to work the second question, but unfortunately the VB has a few bugs in it.
Using Form Events. One of the standard and usually most recommended ways to set up a list in a combo box makes use of the Form_Initialize or Form_Load sort of events. Certain code is executed when the form that contains the combo box is first loaded and during that processing the entries in the combo box are developed.
Your form should look something like the following.
|Now you want to copy the following code into the
This code will fill in the combo box entries with the abbreviations for the six New England states. It will also default to CT because of the .ListIndex command. Setting the ListIndex to zero makes the default the first item in the list. The list numbering starts at zero, and if you want to set a different item as the default you need to number accordingly.
The only disadvantage to this approach seems to come up when you are working with databases and working with multiple records. This list is developed only when the form is initialized and doesn't adjust to certain types of user commands as dynamically as it always needs.
The DropDown Event. It is also possible to make use of the user clicking on the DropDown button at the right end of the combo box to set up. The code for this is entirely similar to the UserForm_Initialize code. It just will be executed each time the user clicks on the drop down triangle/arrow. Though how it executes can be a bit buggy on some computers and in some VB environments. The name of the event also changes slightly between VB IDEs.
The code follows
You should try implementing this to see how it works in practice. You are far more likely to use the first set of coding style on the homework. With the drop down button click event while you get the default item each time you also get the list appended to the previous list each time you click. The following code provides a work-around.
This will cause the list to be appended only if there is nothing already in the list, but the ListIndex will still set the default each time a user clicks it. This should do it for the combo box.