VISUAL BASIC CONTROLS LIST BOXES & COMBO BOXES
LIST BOX & COMBO BOX
Combo box tool
List box tool
(CONTD«.)
A DDING DDING ITEMS TO A A LIST LIST We can add items to a list box at either design time or at runtime. roperty, which is a very A t design time, we can use the List prop handy array of the items in the list box; and at runtime, we can use both the List prop roperty and the AddItem() the AddItem() method. Here·s how we use the List prop roperty in code, .List( .List(index ) [= string ] ListBox We can track the total number of items in a list box using ListCount prop roperty loo p to. We·ll use ListCount as the maximum value to loop roperty as A t runtime, we can either use the indexed List prop detailed previously, or the AddItem() method this way:
Private Sub Form_Load( Form_Load () List1. A ddItem (´ Item 1µ) A ddItem (´Item List1. A ddItem (´ Item 2µ) A ddItem (´Item A ddItem (´Item List1. A ddItem (´ Item 3µ) A ddItem (´Item List1. A ddItem (´ Item 4µ) End Sub
REFERRING
A LIST TO ITEMS IN A LIST BY INDEX
When we add items to a list box, each item is given an index, and we can refer to the item in the list box using this index (for examp example, you can get the item·s ite m·s text by using the List property: List(index)).
The
When the user selects an item in a list box, you can get the selected item·s index with the list box·s ListIndex property.
For example,
first item added to a list box gets the index 0, the next index 1, and so on.
Private Sub Form_Load() List1. A ddItem ("Item 0") A ddItem ("Item 0" List1. A ddItem ("Item 1") A ddItem ("Item 1" List1. A ddItem ("Item 2") A ddItem ("Item 2" List1. A ddItem ("Item 3") A ddItem ("Item 3"
End Sub
RESPONDING
lick C lick
TO LIST BOX EVENTS
And DblC lick lick
y
We use two main events with list boxes: Click and DblClick.
y
How
y
y
we actually use them is up to you, because different programs have different needs. We use the Click event just as we use the Click event event in a button, with a Click event handler.
Here, we display the item in the list box the user has clicked, using the ListIndex property (we can get the selected item·s text with List1.List(ListIndex) or with List1.Text): Private Sub List1_Click() MsgBox " You clicked item " & Str( Str(List1.ListIndex)
End Sub y
nd displaying the selected item is the same for DblClick³ A nd you just add a DblClick handler with the code we want: Private Sub List1_DblClick() MsgBox " You clicked item " & Str( Str(List1.ListIndex)
End Sub
(CONTD«.) ltiselect List Boxes
Mu y
y
List boxes can also be multiselect list boxes, which means the user can select a number of items in the list box. If our list box is a multiselect box, you can determine which items the user has selected by using the Selected property this way: For intLoopIndex = 0 To List1.ListCou List1.ListCount nt - 1 If List1.Selected( List1.Selected(intLoop intLoopIndex) Then ... End If
Next intLoopIndex
REMOVING
A LIST ITEMS FROM A LIST
We can remove items from a list box at design time simp simply by deleting them in the List roperty. prop
A t runtime, you use the RemoveItem() method.
Here·s
an example; example; in this t his case, we add four items, Items 0 through 3 to a list box: Private Sub Form_Load() List1. A ddItem ("Item 0") A ddItem ("Item 0" List1. A ddItem ("Item 1") A ddItem ("Item 1" List1. A ddItem ("Item 2") A ddItem ("Item 2" List1. A ddItem ("Item 3") A ddItem ("Item 3"
End Sub Private Sub Command1_Click() List1.RemoveItem 1
End Sub
SORTING TING A A LIST LIST BOX
We can alp alphabetize the items in a list box by setting its Sorted property to True (it·s False by default) at design time or runtime.
DETERMINIG HOW M A M A NY NY ITEMS A RE IN A IN A LIST BOX
We can use the ListCount property to determine how many items are in a list box.
When setting up up loop loops over the items in a list box, we should note that ListCount is the total number of items in a list, whereas index values start at 0, not 1.
For examp example, Private Sub Command1_Click() Dim intLoop intLoopIndex A Index A s Integer For intLoop intLoopIndex = 0 To List1.Lis List1.ListCoun tCountt - 1 ... Next intLoop intLoopIndex
End Sub
DETERMINING IF A LISTBOX ITEM IS SELECTED
We get the index of the selected item in a list box with the ListIndex property.
If no item is selected, ListIndex will be ²1.
We can get the text of a list·s selected sele cted item as List1.Text or List1.List(List1.ListIndex).
We can use a list box·s Selected array to determine if individual items in the list box are selected or not.
nd we check the Selected array for each item to find A nd the selected item: Private Sub Command1_Click Command1_Click () Dim intLoop intLoopIndex For intLoop intLoopIndex = 0 To List1.Li List1.ListCou stCount nt - 1 If List1.Selected( List1.Selected(intLoop intLoopIndex) Then MsgBox " You selected " & List1.List( List1.List(intLoop intLoopIndex) End If Next intLoop intLoopIndex End Sub
USING
MULTISELECT LIST BOXES
A multiselect A multiselect list box allows the user to select a number of items at one time.
We make a list box into a multiselect list box with wi th the MultiSelect property.
The user can then select multip multiple items using the Shift and Ctrl keys.
Here are the possible settings for MultiSelect: 0³ Multiple selection isn·t allowed (this is the default). 1³Simple multiple selection. A mouse click or pressing the spacebar selects or deselects an item in the list. (Arrow keys move the focus.) 2³Extended multiple selection. Pressing the Shift key and clicking the mouse or pressing the Shift key and one of the arrow keys extends the selection from the previously selected item to the current item. Pressing the C trl trl key andclicking the mouse selects or deselects an item in the list.
M AK ING ING LIST BOXES SCROLL HORIZONT A LLY LLY
If we break up up the list into columns using the olumns property. C ol
that prop roperty is set to 0, the default, the list box presents just a vertical list to the user.
When you set the Columns property to another value, the list box displays its items in that number of columns co lumns instead.
W hen
USING
CHEC CH ECK K M A RK S IN A IN A LIST LIST BOX
When you use checkmark list boxes, selected items app appear ear with a checkmark in front
of them. You can make a list box into a checkmark list box with its Style property,
which can take these values: 0³Standard list box (the default) 1³C heckmark list box
CRE A TING TING A A LIST LIST BOX
We can use the Clear method to clear a list box.
We just use clear like this: List.C lear. lear.
Here·s how that looks in code; in this case, we·re clearing a list box, List1, when the user clicks a command button:
Private Sub Command1_Click() y
List1.Clear
End Sub
CRE A TING TING SIMPLE COMBO BOXES, DROP-DOWN COMBO BOXES & D ROPDOWN LIST COMBO BOXES
Combo boxes are those controls that usually disp display a text box and a drop drop-down list.
In fact, we might think there is only one kind of pes, and we combo box, but there are really three ty typ select which typ type we want with the combo box·s Style property.
The default typ type of combo box is probably what we think of when we think of combo boxes, because, as mentioned, it is made up up of a text box and a drop dro pdown list.
However, we can also have combo boxes where the list doesn·t drop drop down.
(CONTD«)
Here are the settings for the combo box Style property:
VbComboDropDown³0; drop-down combo box. Includes a drop-down list and a text box. The user can select from the list or type in the text box. (This the default.) VbComboSimple³1; simple combo box. Includes a text box and a list, which doesn·t drop down. The user can select from the list or type in the text box. The size of a sim ple combo box includes both the edit and list portions. By default, a simple combo box is sized so that none of the list is dis played. Increase the Height property to display more of the list. VbComboDrop-DownList³2; drop-down list. This style allows a selection only from the drop-down list. This is a good one to keep in mind when you want to restrict the user·s in put; however, if you want to use this one, you should also consider simple list boxes. The selected selecte d item appears in the (read-only) text box.
A DDING DDING ITEMS TO A A COMBO COMBO BOX
A combo A combo box is a combination of a text box and a list box, so at design time, we can change the text in the text box part by changing the Text property. We change the items in the list box part with the List property. A t runtime, we can add items to a combo box using the AddItem() method, which adds items to the list box part. We can also add items to the list box using the List prop roperty, which is an indexed array of items in the list box. If we want to set text in the text box, set the combo box·s Text property. Here·s an examp example, Private Sub Form_Load() Combo1. A ddItem ("Item 0") A ddItem ("Item 0" Combo1. A ddItem ("Item 1") A ddItem ("Item 1" Combo1. A ddItem ("Item 2") A ddItem ("Item 2" Combo1. A ddItem ("Item 3") A ddItem ("Item 3"
End sub
RESPONDING
TO COMBO BOX SELECTIONS
Combo boxes are combinations of text boxes and list boxes, and that combination means that there are two sets of inp input events: Change events when the user types into the text box and Click or DblClick when the user uses the mouse.
Note that, unlike standard list boxes, you cannot make multip multiple selections in a combo box·s list box.
(CONTD«.)
C hange y
y
Events
When the user changes the text in a combo box, a Change event occurs, just as it does when the user typ types in a standard s tandard text box. We can read the new text in the text box with the Text property Private Sub Form_Load() Combo1. A ddItem ("Item 0") A ddItem ("Item 0" Combo1. A ddItem ("Item 1") A ddItem ("Item 1" Combo1. A ddItem ("Item 2") A ddItem ("Item 2" Combo1. A ddItem ("Item 3") A ddItem ("Item 3"
End Sub Private Sub Combo1_Change() MsgBox "New text is: " & Combo1.Text
End Sub
(CONTD«)
lick C lick y
y
Events
We can also get Click events when the user makes a selection in the list box using the mouse. We can determine which item the user clicked using the combo·s ListIndex prop roperty (which holds the index of the clicked item) or get that item·s text using the Text prop roperty, because when we click an item, it is made the new selected s elected item in the text box.
(CONTD«.) DblC lick lick Events
y
y
We might exp expect that where there are Click events there are DblClick events, and that·s true³but for simp simple combo boxes only (Style = VbComboSimple, where VbComboSimple is a Visual Basic constant that equals 1). When you click an item in the list part of a combo box once, the list closes, so it·s imp impossible to doubleclick an item³excep item³except in simp simple combo boxes, where the list stays op open at all times
REMOVING
ITEMS FROM A A COMBO COMBO BOX
Just as with list boxes, you can remove items from combo boxes using the RemoveItem() method.
We just pass the index of the item you want to remove from the combo box·s list to Remov Private Sub Command1_Click () Combo1.RemoveItem 1 End SubeItem().
THE CURRENT SELECTION IN A COMBO BOX GETTING
When we make a selection in a combo box, that new selection app appears ears in the combo box·s text box, so it·s easy to get the current selection³we just use the combo box·s Text prop roperty.
We can also get the currently selected item·s index in the combo box·s list using the ListIndex property. If no selection is made (for instance, when the form first loads and the combo·s text box is emp empty), this prop roperty will return ²1.
SORTING A COMBO BOX
We can leave the work up up to the combo box itself if you set its Sorted prop roperty to True
Default is false in the sorted prop roperty
CLEARING A COMBO BOX
the current items there one by one with RemoveItem()
No, we can clear a whole combo box at once with the Clear() method. Private Sub Command1_Click () Combo1.Clear End Sub
LOCKING A COMBO BOX You can lock a combo box by setting se tting its Locked property to T r r ue.
locked, the user cannot enter text in the combo·s text box and cannot make selections from thecombo·s list (although if the list is drop drop-down, it will still op open).
of ´lockingµ a However, when programmers think of ´ combo box, it·s not usually the Locked property thatthey thatthey want.
W hen