![]() |
|
|
Manipulating the Different Controls and Components In this section, we'll take a closer at the many controls available in Visual Basic. Most of these are located in the Tool Box and can be easily drawn on your forms. Combo Boxes and List Boxes Frames, Option Buttons and Check Boxes Using the Timer Control Adding Additional Controls to your Tool BoxCombo Boxes and List Boxes Combo Boxes and List Boxes are two different types of systems that provide data to the user in a list form. Each of the two also has the option of becoming a dropdown list. Take a look at these samples:
List boxes limit the user to only what is on the list, whereas combo boxes allow the user to enter data that is then searched for in the last, or better yet, added to the list. All you have to do to insert a list box or combo box on your form is click on their icons and then draw them on the form.
To add items to a combo box or list box, simply edit the List property of either and enter as many items as you want to appear on the list. This can also be done by using code: lstMyList.AddItem "Black" This adds the item Black to the MyList list. You may wish to use this technique to add items to your lists at the click of a button during run-time. Or you may even want to give your user the option to add custom items to your lists.
Sorting is another property of list/combo boxes that you should know about. If the Sorted property of your list is set to True then your current items will be sorted. Also, any items that are added, are automatically placed in their proper sorted position. To get over this, you can specify an index and force an added item to appear wherever you want it to. This is done by adding the index to the end of the code above: lstMyList.AddItem "Black", 4 Note that the index of the first item is 0. The property that is responsible for returning the index of the current selection is called ListIndex. If no item is selected, ListIndex returns -1. This can be useful for checking to see whether a user entry is actually available in the list/combo box. Another useful property is NewIndex. This tells you the position of the last item that was added. Take a look at this example that highlights the item that was added last in a sorted list box:
lstMyList.AddItem "Black" After adding "Black" to the list, it then selects it!
To remove an item from a list box or combo box you use the RemoveItem method. Here is an example: lstMyList.RemoveItem, 4 This removes the item at position (index) 4 in the list MyList. |