Sie sind auf Seite 1von 27

VISUAL BASIC CONTROLS

LIST BOXES & COMBO BOXES

LIST BOX & COMBO BOX

Combo box tool

List box tool

(CONTD.)

ADDING ITEMS TO A LIST


We can add items to a list box at either design time or at runtime. At design time, we can use the List property, which is a very handy array of the items in the list box; and at runtime, we can use both the List property and the AddItem() method. Heres how we use the List property in code, ListBox.List(index) [= string] We can track the total number of items in a list box using ListCount property Well use ListCount as the maximum value to loop to. At runtime, we can either use the indexed List property as detailed previously, or the AddItem() method this way:
Private Sub Form_Load() List1.AddItem (Item 1) List1.AddItem (Item 2) List1.AddItem (Item 3) List1.AddItem (Item 4) End Sub

REFERRING 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 example, you can get the items text by using the List property: List(index)). The first item added to a list box gets the index 0, the next index 1, and so on. When the user selects an item in a list box, you can get the selected items index with the list boxs ListIndex property. For example,

Private Sub Form_Load()


List1.AddItem ("Item 0") List1.AddItem ("Item 1") List1.AddItem ("Item 2") List1.AddItem ("Item 3")

End Sub

RESPONDING TO LIST BOX EVENTS


Click And DblClick


y y y y

We use two main events with list boxes: Click and DblClick. How 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 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 items text with List1.List(ListIndex) or with List1.Text): Private Sub List1_Click()
MsgBox "You clicked item " & Str(List1.ListIndex)

End Sub And displaying the selected item is the same for DblClick you just add a DblClick handler with the code we want: Private Sub List1_DblClick()
MsgBox "You clicked item " & Str(List1.ListIndex)

End Sub

(CONTD.)

Multiselect List Boxes


List boxes can also be multiselect list boxes, which means the user can select a number of items in the list box. y If our list box is a multiselect box, you can determine which items the user has selected by using the Selected property this way:
y

For intLoopIndex = 0 To List1.ListCount - 1


If List1.Selected(intLoopIndex) Then ... End If

Next intLoopIndex

REMOVING ITEMS FROM A LIST


We can remove items from a list box at design time simply by deleting them in the List property. At runtime, you use the RemoveItem() method. Heres an example; in this case, we add four items, Items 0 through 3 to a list box:

Private Sub Form_Load()


List1.AddItem ("Item 0") List1.AddItem ("Item 1") List1.AddItem ("Item 2") List1.AddItem ("Item 3")

End Sub Private Sub Command1_Click()


List1.RemoveItem 1

End Sub

SORTING A LIST BOX


We can alphabetize the items in a list box by setting its Sorted property to True (its False by default) at design time or runtime.

DETERMINIG HOW MANY ITEMS ARE IN A LIST BOX


We can use the ListCount property to determine how many items are in a list box. When setting up 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 example,

Private Sub Command1_Click()


Dim intLoopIndex As Integer For intLoopIndex = 0 To List1.ListCount - 1 ... Next 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 lists selected item as List1.Text or List1.List(List1.ListIndex). We can use a list boxs Selected array to determine if individual items in the list box are selected or not. And we check the Selected array for each item to find the selected item:

Private Sub Command1_Click () Dim intLoopIndex For intLoopIndex = 0 To List1.ListCount - 1 If List1.Selected(intLoopIndex) Then MsgBox "You selected " & List1.List(intLoopIndex) End If Next intLoopIndex End Sub

USING MULTISELECT LIST BOXES


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 the MultiSelect property. The user can then select multiple items using the Shift and Ctrl keys. Here are the possible settings for MultiSelect:

0Multiple selection isnt allowed (this is the default). 1Simple multiple selection. A mouse click or pressing the spacebar selects or deselects an item in the list. (Arrow keys move the focus.) 2Extended 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 Ctrl key andclicking the mouse selects or deselects an item in the list.

MAKING LIST BOXES SCROLL HORIZONTALLY


If we break up the list into columns using the Columns property. When that property 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 instead.

USING CHECKMARKS IN A LIST BOX


When you use checkmark list boxes, selected items appear 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:

0Standard list box (the default) 1Checkmark list box

CREATING A LIST BOX


We can use the Clear method to clear a list box. We just use clear like this: List.Clear. Heres how that looks in code; in this case, were clearing a list box, List1, when the user clicks a command button:

Private Sub Command1_Click()


y

List1.Clear

End Sub

CREATING SIMPLE COMBO BOXES, DROP-DOWN COMBO BOXES & DROPDOWN LIST COMBO BOXES
Combo boxes are those controls that usually display a text box and a drop-down list. In fact, we might think there is only one kind of combo box, but there are really three types, and we select which type we want with the combo boxs Style property. The default type of combo box is probably what we think of when we think of combo boxes, because, as mentioned, it is made up of a text box and a dropdown list. However, we can also have combo boxes where the list doesnt drop down.

(CONTD)

Here are the settings for the combo box Style property:
VbComboDropDown0; 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.) VbComboSimple1; simple combo box. Includes a text box and a list, which doesnt drop down. The user can select from the list or type in the text box. The size of a simple combo box includes both the edit and list portions. By default, a simple combo box is sized so that none of the list is displayed. Increase the Height property to display more of the list. VbComboDrop-DownList2; 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 users input; however, if you want to use this one, you should also consider simple list boxes. The selected item appears in the (read-only) text box.

ADDING ITEMS TO A COMBO BOX


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. At 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 property, which is an indexed array of items in the list box. If we want to set text in the text box, set the combo boxs Text property. Heres an example,
Private Sub Form_Load()
Combo1.AddItem ("Item 0") Combo1.AddItem ("Item 1") Combo1.AddItem ("Item 2") Combo1.AddItem ("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 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 multiple selections in a combo boxs list box.

(CONTD.)

Change Events
When the user changes the text in a combo box, a Change event occurs, just as it does when the user types in a standard text box. y We can read the new text in the text box with the Text property
y

Private Sub Form_Load()


Combo1.AddItem ("Item 0") Combo1.AddItem ("Item 1") Combo1.AddItem ("Item 2") Combo1.AddItem ("Item 3")

End Sub Private Sub Combo1_Change()


MsgBox "New text is: " & Combo1.Text

End Sub

(CONTD)

Click Events
We can also get Click events when the user makes a selection in the list box using the mouse. y We can determine which item the user clicked using the combos ListIndex property (which holds the index of the clicked item) or get that items text using the Text property, because when we click an item, it is made the new selected item in the text box.
y

(CONTD.)

DblClick Events
We might expect that where there are Click events there are DblClick events, and thats truebut for simple combo boxes only (Style = VbComboSimple, where VbComboSimple is a Visual Basic constant that equals 1). y When you click an item in the list part of a combo box once, the list closes, so its impossible to doubleclick an itemexcept in simple combo boxes, where the list stays open at all times
y

REMOVING ITEMS FROM A 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 boxs list to Remov

Private Sub Command1_Click() Combo1.RemoveItem 1 End SubeItem().

GETTING THE CURRENT SELECTION IN A COMBO BOX


When we make a selection in a combo box, that new selection appears in the combo boxs text box, so its easy to get the current selectionwe just use the combo boxs Text property. We can also get the currently selected items index in the combo boxs list using the ListIndex property. If no selection is made (for instance, when the form first loads and the combos text box is empty), this property will return 1.

SORTING A COMBO BOX


We can leave the work up to the combo box itself if you set its Sorted property to True Default is false in the sorted property

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 its Locked property to True. When locked, the user cannot enter text in the combos text box and cannot make selections from thecombos list (although if the list is drop-down, it will still open). However, when programmers think of locking a combo box, its not usually the Locked property thatthey want.

Das könnte Ihnen auch gefallen