|
Getting Information from the User
TABLE OF CONTENTS
The ListBox
The ListBox control displays a list of choices from which the user can select one or more items. ListBoxes that permit the user to select only one choice are referred to as Single-Choice or Simple ListBoxes. ListBoxes that permit the user to select one or more choices are referred to as MultiSelect ListBoxes.
Figure 1. ListBox Example
Features of the ListBox
- It displays scroll bars if theListBox isn't tall enough or wide enough to display all its data in the space provided.
- Multiple columns of choices can be displayed simultaneously
- It can display check boxes in front of the items displayed.
- Choices can be automatically sorted
- Choices can be added and removed dynamically
- A MultiSelect ListBox, if the MultiSelect property is set to 1 in the Properties sheet, will respond to a mouse click or <spacebar> for selecting and deselecting items.
- A MultiSelect ListBox, if the MultiSelect property is set to 2 will respond to pressing <Shift> and then clicking the mouse as extending the range of selections from the cursor position of the previous selected item. If <Ctrl> is pressed simultaneously with clicking the mouse, it will cause the item clicked on to be selected or de-selected.
Go Back to Top
How You Get Items In and Out of the ListBox via Code
By using the methods and properties associated with the ListBox control, what's displayed in the ListBox can be changed.
Method or Property
|
What It Does
|
|
AddItem
|
Adds an item to the list
|
|
Clear
|
Clears the entire list of items
|
|
Index
|
Specifies a number associated with each "List array" item
|
|
List
|
Functions as a string array for holding the items used by theListBox
|
|
ListIndex
|
Contains the index of the selected ListBox item. If an item isn't selected, its value is -1
|
|
ListCount
|
Holds the total number of items in the ListBox
|
|
NewIndex
|
Holds index of the last item add to the ListBox
|
|
RemoveItem
|
Removes a single item from the list
|
|
SelCount
|
Contains the number of ListBox items selected
|
|
SetFocus
|
Sets the focus to the ListBox
|
|
The best time to initialize the ListBox is when the Form is first loaded. So, in most programs, the initialization of the ListBox is associated with a Form_Load() event.
Go Back to Top
Properties of the ListBox That Can Be Manipulated or Used Via Code
- In a simple, single-choice ListBox, VB stores the user's choice in the ListBox's Text property.
- As each item is added to a ListBox, an array is created containing an Index value for each item in the array. So, creating a ten-item ListBox results in a ten-element array with Indexes starting at zero for the first choice in the list and increasing by one until the last choice, which is nine.
- In a "MultiSelect" ListBox the Text property contains the last value selected, not all the items selected. For a MultiSelect ListBox a Selected array contains all the text of all the choices selected.
- The ListIndex property contains the Index of the choice selected. The ListCount property holds a running total of the number of items in the list.
Go Back to Top
How These ListBox Properties/Methods Relate To One Another
When a ListBox is created, an Index array and a List array are created. Using our example above, where the ListBox is named
Index(an array)
|
List (an array)
|
Way to represent the List array item as a variable
|
|
0
|
Speedy Gonzales
|
lstAssociates.List(0)
|
|
1
|
Bugs Bunny
|
lstAssociates.List(1)
|
|
2
|
George W. Bush
|
lstAssociates.List(2)
|
|
3
|
Wiley Coyote
|
lstAssociates.List(3)
|
|
4
|
Daffy Duck
|
lstAssociates.List(4)
|
|
(in the properties sheet) lstAssociates, we'd have the following:
Notice that the way to represent the ListBox item as a variable combines the List value with the Index value. So if we printed the value of lstAssociates.List(2), the value that would be printed is George W. Bush.
Here's what some of the other property values would be for our ListBox example.
- lstAssociates.ListCount = 5
- lstAssociates.ListIndex = -1 (signifying that no choice has been selected; if one hadListIndex would take on the Index value)
- lstAssociates.NewIndex = 4
- lstAssociates.Text = "" (No item was selected, so Text is EMPTY)
So if we wanted to check if the name the user enters in the TextBox of our example, we'd construct a loop which would check against all lstAssociate.List(i), where i would go from 0 to lstAssociates.ListCount –1. Some other examples of how we use these properties and methods follow.
Go Back to Top
Example Code—Using the ListBox Methods
Suppose we create a ListBox named "LstAssociates", then what follows are examples of code that might be used to manipulate LstAssociates.
Go Back to Top
Setting Up the ListBox(Adding Items to a ListBox):
Private Sub Form_Load()
LstAssociates.AddItem ("Choice 1")
LstAssociates.AddItem ("Choice 2")
LstAssociates.AddItem ("Choice 3")
LstAssociates.AddItem ("Choice 4")
End Sub
Go Back to Top
Removing Items from a ListBox:
To remove the third item in a ListBox, you'd use the following statement--
Go Back to Top
To remove a selected item from a list (note that RemoveItem method always refers to a ListIndex):
Go Back to Top
Clearing All Items from aListBox:
Go Back to Top
Determining which Item Was Selected (in a SimpleListBox) Using ListIndex:
Private Sub LstAssociates_Click()
MsgBox "You selected item " & Str(LstAssociates.ListIndex)
End Sub
NOTE: If no item is selected, a value of –1 is returned.
Go Back to Top
Determining which Item Was Selected (in a SimpleListBox) Using theListBox's Text Property:
Private Sub LstAssociates_Click ()
If LstAssociates.Text = "Choice 1" Then
Program Statements
End If
End Sub
Go Back to Top
Determining which Items Were Selected (in a MultiSelectListBox):
Dim intLoopIndex as Integer
For intLoopIndex = 0 to LstAssociates.ListCount – 1
If lstAssociates.Selected(intLoopIndex) then
programming statements
End If
Next intLoopIndex
NOTE: The Selected array stores which choices have been selected in a MultiSelectListBox.
Go Back to Top
Sorting a ListBox:
You can alphabetize a list of items contained in a ListBox by setting its Sorted property to True. Note, however, if you do this that you will affect the Indexes of the various items.
Go Back to Top
TextBox
A TextBox can display text or accept user input directly. Its Text property contains any text you want the user to see; its Locked property determines whether the user is allowed to enter data into the TextBox.
Go Back to Top
ComboBox
A Combo Box consists of a TextBox and a ListBox, and it works much like theListBox except that the user can add items to a ComboBox rather than just being restricted to scrolling and selecting items.
Figure 2. TextBox and ComboBox Example
There are three types of ComboBoxes:
- The DropDown ComboBox, which contains a text box and drop down list. It takes up one line on your form unless the user opens the drop-down portion of theComboBox using the down arrowhead icon. The user can type in values in the TextBox portion as well as selecting items in the drop-down portion of theComboBox
- The Simple ComboBox, which contains a text box and a list that doesn't drop down. The user can add items to the list portion of theComboBox.
- The Drop-DownList ComboBox, which contains a drop-down list to which the user cannot add additional items. The main difference between it and a ListBox is that the user must click on the down arrowhead icon to see the list.
The ComboBox's methods are identical to those of the ListBox (see the ListBox section above).
Go Back to Top
Return to the Visual Basic Page
|