listbox,combobox,visual basic 6.0
Post on 13-Jul-2015
2.073 Views
Preview:
TRANSCRIPT
5/12/2018 listbox,combobox,Visual basic 6.0 - slidepdf.com
http://slidepdf.com/reader/full/listboxcomboboxvisual-basic-60 1/27
VISUAL BASIC CONTROLSLIST BOXES & COMBO BOXES
5/12/2018 listbox,combobox,Visual basic 6.0 - slidepdf.com
http://slidepdf.com/reader/full/listboxcomboboxvisual-basic-60 2/27
LIST BOX & COMBO BOX
Combo box tool List box tool
5/12/2018 listbox,combobox,Visual basic 6.0 - slidepdf.com
http://slidepdf.com/reader/full/listboxcomboboxvisual-basic-60 3/27
(CONTD«.)
5/12/2018 listbox,combobox,Visual basic 6.0 - slidepdf.com
http://slidepdf.com/reader/full/listboxcomboboxvisual-basic-60 4/27
A DDING ITEMS TO A LIST
We can add items to a list box at either design time or at runtime.
A t design time, we can use the List property, which is a veryhandy array of the items in the list box; and at runtime, we canuse both the List property and the AddItem() method.
Here·s how we use the List property in code,
ListBox .List(index ) [= string ]
We can track the total number of items in a list box usingListCount property
We·ll use ListCount as the maximum value to loop to.
A t runtime, we can either use the indexed List property asdetailed previously, or the
AddItem() method this way:Private Sub Form_Load()
List1. A ddItem (´Item 1µ)
List1. A ddItem (´Item 2µ)
List1. A ddItem (´Item 3µ)
List1. A ddItem (´Item 4µ)
End Sub
5/12/2018 listbox,combobox,Visual basic 6.0 - slidepdf.com
http://slidepdf.com/reader/full/listboxcomboboxvisual-basic-60 5/27
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 usingthis index (for example, you can get the item·s 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 item·s index with the list box·s ListIndex
property.
For example,Private Sub Form_Load()
List1. A ddItem ("Item 0")
List1. A ddItem ("Item 1")
List1. A ddItem ("Item 2")
List1. A ddItem ("Item 3")
End Sub
5/12/2018 listbox,combobox,Visual basic 6.0 - slidepdf.com
http://slidepdf.com/reader/full/listboxcomboboxvisual-basic-60 6/27
RESPONDING TO LIST BOX EVENTS
C lick And DblC lick
y We use two main events with list boxes: Click and DblClick.y How we actually use them is up to you, because different
programs have different needs.
y We use the Click event just as we use the Click event in
a button, with a Click event handler.
y Here, we display the item in the list box the user has clicked,
using the ListIndex property (we can get the selecteditem·s text with List1.List(ListIndex) or with List1.Text):
Private Sub List1_Click()
MsgBox " You clicked item " & Str(List1.ListIndex)
End Sub
y A nd 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
5/12/2018 listbox,combobox,Visual basic 6.0 - slidepdf.com
http://slidepdf.com/reader/full/listboxcomboboxvisual-basic-60 7/27
(CONTD«.)
Multiselect List Boxes
y 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 theSelected property this way:
For intLoopIndex = 0 To List1.ListCount - 1
If List1.Selected(intLoopIndex) Then
...
End If
Next intLoopIndex
5/12/2018 listbox,combobox,Visual basic 6.0 - slidepdf.com
http://slidepdf.com/reader/full/listboxcomboboxvisual-basic-60 8/27
REMOVING ITEMS FROM A LIST
We can remove items from a list box at designtime simply by deleting them in the List
property.
A t runtime, you use the RemoveItem()
method.
Here·s an example; in this case, we add four
items, Items 0 through 3 to a list box:Private Sub Form_Load()
List1. A ddItem ("Item 0")
List1. A ddItem ("Item 1")
List1. A ddItem ("Item 2")
List1. A ddItem ("Item 3")
End Sub
Private Sub Command1_Click()
List1.RemoveItem 1
End Sub
5/12/2018 listbox,combobox,Visual basic 6.0 - slidepdf.com
http://slidepdf.com/reader/full/listboxcomboboxvisual-basic-60 9/27
SORTING A LIST BOX
We can alphabetize the items in a list box bysetting its Sorted property to True (it·s False
by default) at design time or runtime.
5/12/2018 listbox,combobox,Visual basic 6.0 - slidepdf.com
http://slidepdf.com/reader/full/listboxcomboboxvisual-basic-60 10/27
DETERMINIG HOW M A NY ITEMS A RE 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 totalnumber of items in a list, whereas index values
start at 0, not 1.
For example,Private Sub Command1_Click()
Dim intLoopIndex A s Integer
For intLoopIndex = 0 To List1.ListCount - 1
...
Next intLoopIndex
End Sub
5/12/2018 listbox,combobox,Visual basic 6.0 - slidepdf.com
http://slidepdf.com/reader/full/listboxcomboboxvisual-basic-60 11/27
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 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.
A nd 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
5/12/2018 listbox,combobox,Visual basic 6.0 - slidepdf.com
http://slidepdf.com/reader/full/listboxcomboboxvisual-basic-60 12/27
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:
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 key andclicking
the mouse selects or deselects an item in the list.
5/12/2018 listbox,combobox,Visual basic 6.0 - slidepdf.com
http://slidepdf.com/reader/full/listboxcomboboxvisual-basic-60 13/27
M AK ING LIST BOXES SCROLL
HORIZONT A LLY
If we break up the list into columns using the
C olumns property.
W hen 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.
5/12/2018 listbox,combobox,Visual basic 6.0 - slidepdf.com
http://slidepdf.com/reader/full/listboxcomboboxvisual-basic-60 14/27
USING CHECK M A RK S 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:
0³Standard list box (the default)
1³C heckmark list box
5/12/2018 listbox,combobox,Visual basic 6.0 - slidepdf.com
http://slidepdf.com/reader/full/listboxcomboboxvisual-basic-60 15/27
CRE A TING A LIST BOX
We can use the Clear method to clear a list box. We just use clear like this: List.C 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
5/12/2018 listbox,combobox,Visual basic 6.0 - slidepdf.com
http://slidepdf.com/reader/full/listboxcomboboxvisual-basic-60 16/27
CRE A TING SIMPLE COMBO BOXES,
DROP-DOWN COMBO BOXES & DROP-
DOWN 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 weselect which type we want with the combo box·s
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 drop-
down list.
However, we can also have combo boxes where the
list doesn·t drop down.
5/12/2018 listbox,combobox,Visual basic 6.0 - slidepdf.com
http://slidepdf.com/reader/full/listboxcomboboxvisual-basic-60 17/27
(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 thelist 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-DownList³2; drop-down list. This style
allows a selection only from the drop-down list. This is a good oneto keep in mind when you want to restrict the user·s 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.
5/12/2018 listbox,combobox,Visual basic 6.0 - slidepdf.com
http://slidepdf.com/reader/full/listboxcomboboxvisual-basic-60 18/27
A DDING 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.
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 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 box·s Text
property.
Here·s an example,Private Sub Form_Load()
Combo1. A ddItem ("Item 0")
Combo1. A ddItem ("Item 1")
Combo1. A ddItem ("Item 2")
Combo1. A ddItem ("Item 3")
End sub
5/12/2018 listbox,combobox,Visual basic 6.0 - slidepdf.com
http://slidepdf.com/reader/full/listboxcomboboxvisual-basic-60 19/27
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 box·s
list box.
5/12/2018 listbox,combobox,Visual basic 6.0 - slidepdf.com
http://slidepdf.com/reader/full/listboxcomboboxvisual-basic-60 20/27
(CONTD«.)
C hange Events
y 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
Private Sub Form_Load()
Combo1. A ddItem ("Item 0")
Combo1. A ddItem ("Item 1")
Combo1. A ddItem ("Item 2")Combo1. A ddItem ("Item 3")
End Sub
Private Sub Combo1_Change()
MsgBox "New text is: " & Combo1.Text
End Sub
5/12/2018 listbox,combobox,Visual basic 6.0 - slidepdf.com
http://slidepdf.com/reader/full/listboxcomboboxvisual-basic-60 21/27
(CONTD«)
C lick Eventsy 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 combo·s ListIndex property (which holds theindex of the clicked item) or get that item·s text using
the Text property, because when we click an item, it
is made the new selected item in the text box.
5/12/2018 listbox,combobox,Visual basic 6.0 - slidepdf.com
http://slidepdf.com/reader/full/listboxcomboboxvisual-basic-60 22/27
(CONTD«.)
DblC lick Events
y We might expect that where there are Click events
there are DblClick events, and that·s true³but
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 it·s impossible to double-
click an item³except in simple combo boxes, where
the list stays open at all times
5/12/2018 listbox,combobox,Visual basic 6.0 - slidepdf.com
http://slidepdf.com/reader/full/listboxcomboboxvisual-basic-60 23/27
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 box·s list to RemovPrivate Sub Command1_Click()
Combo1.RemoveItem 1
End SubeItem().
5/12/2018 listbox,combobox,Visual basic 6.0 - slidepdf.com
http://slidepdf.com/reader/full/listboxcomboboxvisual-basic-60 24/27
GETTING THE CURRENT SELECTION IN A
COMBO BOX
When we make a selection in a combo box, that
new selection appears in the combo box·s text
box, so it·s easy to get the current selection³we
just use the combo box·s Text property.
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 empty), this property willreturn ²1.
5/12/2018 listbox,combobox,Visual basic 6.0 - slidepdf.com
http://slidepdf.com/reader/full/listboxcomboboxvisual-basic-60 25/27
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
5/12/2018 listbox,combobox,Visual basic 6.0 - slidepdf.com
http://slidepdf.com/reader/full/listboxcomboboxvisual-basic-60 26/27
CLEARING A COMBO BOX
the current items there one by one withRemoveItem()
No, we can clear a whole combo box at once with the
Clear() method.Private Sub Command1_Click()
Combo1.Clear
End Sub
5/12/2018 listbox,combobox,Visual basic 6.0 - slidepdf.com
http://slidepdf.com/reader/full/listboxcomboboxvisual-basic-60 27/27
LOCKING A COMBO BOX
You can lock a combo box by setting its Locked
property to T r ue.
W hen locked, the user cannot enter text in the
combo·s text box and cannot make selections fromthecombo·s list (although if the list is drop-down,
it will still open).
However, when programmers think of ´lockingµ a
combo box, it·s not usually the Locked property
thatthey want.
top related