Creating List Boxes and Combo Boxes
Both List Boxes and Combo Boxes allow you to have a list of items form which the user can make a selection. There are several types of list boxes and combo boxes, including simple list boxes, simple combo boxes, drop-down combo boxes, and drop-down lists.
ListsBox controls, and ComboBox controls have most of the same properties and operate in a sillier fashion. One exception is that a combo box control has a DropDownStyle property, which determines whether or not the list box also has a text box for user entry and whether or not the list will drop down. Both list boxes and combo boxes have a great feature. If the box is too small to display all the items in the list at one time, VB automatically adds a scroll bar: the scrolling is handled automatically. When you add a list control to a form, choose the style according to the space you have available and how you want the box to operate. Do you want the user to select from an existing list? If so, use a simple list box or a drop-down list (ControlBox DropDownStyle = DropDownList). Do you want the user to be able to type a new entry if necessary? In the is case, use on of the two styles with an added text box the drop-down combo (DropDownStyle = DropDown) or the simple combo box (DropDownStyle = Simple).
At design time, the behavior of list boxes and combo boxes differs. For list boxes, Visual Basic displays the Name property in the control; for combo boxes, the Text property displays, which is blank by default. don't spend any time trying to make a list box appear empty during design time: the box will appear empty at run time. Combo boxes have a Text property, which you can set at design time if you wish. List boxes also have a Text property, but you can access it only at run time.
The list of items that display in a list box or combo box is a called the collection. VB collections are objects that have properties and methods to allow you to add items, remove items, refer to individual elements, count the items, and clear the collection. In the sections that follows, you will learn to maintain and refer to the Items collection. You can refer to the items in a collection by an index, which is zero based. For example, if a collection holds 10 items, the indexes to refer to the items range from 0 to 9. To refer to the first item in the Items collection, use Items(0).
Filling a List During Design Time
The Items property, which is a collection holds the list of items for a list box or combo box. To define the Items collection at design time, select the control and scroll the Properties window to the Items property and type the ellipses button to open the String Collection Editor and type your list items, ending each line with the Enter key. Click OK when finished. You can open the editor again to modify the list, if you wish.
Filling a List During Run Time
You can use several methods to fill the Items collection of a list box and combo box. If you know the list contents at design time and the list never changes, you can define the Items in the Properties windows. If you must add items to the list during run-time (program execution), you will use the Items.Add or Items.Insert method in an event procedure.
To add an item to a list at run-time, use the Items.Add method. You can choose to add a variable, a constant, the contents of the text box at the top of a combo box, or the Text property of another control.
Using the Items.Add Method ItemValue is the string value to add to the list. If the value is a sting literal, enclose it in quotation marks. The new item generally goes at the end of the list. However, you can alter the placement by setting the control's Sorted property to True. Then the new item will be placed alphabetically in the list.
lsbSchools.Items.Add("Gulf Coast High School") lsbSchools.Items.Add("Naples High School") lsbSchools.Items.Add(txtSchools.Text) cboMajors.Items.Add(cboMajors.Text) cboMajors.Items.Add(strMajors)
When the user types a new value in the text box portion of a combo box, that item is not automatically added to the list. If you want to add the newly entered text to the list, use the Items.Add method:
or the preferable form:
With cboClass .Items.Add(.Text) End With
You can add the contents of a text box to a list box
Using the Items.Insert Method You can choose the location for a new item added to the list. In the Items.Insert method, you specify the index position for the new item. The index position is zero based. To insert a new item in the first position, use index position = 0.
lsbSchools.Items.Insert(0, "Gulf Coast High School") cboMajors.Items.Insert(1, cboMajors.Text)
If you choose the index position of an item using the Insert method, do not set the list control's Sorted property to True. A sorted list is always sorted into alphabetic order, regardless of any other order that you request.
Adding Items Using Input Boxes
You can use an Input Box to get information from the user to add a new item to a List Box or Combo Box. Use the following to add an item with an input box.
Dim strSchool As String strSchool = InputBox("Please enter the new High School name.", "Add New School", "Enter school name here") If strSchool <> "" Then lsbHigh.Items.Add(strSchool) Else 'Cancel was pressed or missing name - do nothing End If
Managing List and Combo Boxes
The SelectedIndex Property
When a project is running and the user selects (highlights) an item from the list, the index number of that item is stored in the SelectedIndex property of the list box. Recall that the index for the first item in the list is 0. If not list item is selected, the SelectedIndex property is set to negative 1 (-1).
You can use the SelectedIndex property to select an item in the list or deselect all item in code.
'Select the fourth item in list lsbSchools.SelectedIndex = 3
'Deselect all items in list. 'lsbSchools.SelectedIndex = -1
The Items.Count Property
You can use the Count property of the Items collection to determine the number of items in the list. Items.Count is also handy when you need to display the count at some point in your project. Remember: Items.Count is also one more than the highest possible SelectedIndex, since the indexes begin with 0. For example, if there are five items in a list, Items.Count is 5 and highest index is 4.
|Items.SelectedIndex||Items.Count = 5|
|(0)||Gulf Coast High School|
|(1)||Naples High School|
|(2)||Barron Collier High School|
|(3)||Lely High School|
|(4)||Golden Gate High School|
intTotalItems = lsbSchools.Items.Count or Msgbox("The number of schools in the list is " & labSchools.Items.Count, vbOkonly, "Number of Schools")
Removing an Item from a List
You can remove individual items from a list either by specifying the index of the item or the text of the item. Use the Items.RemoveAt method to remove an item by index and the Items.Remove method to remove by specifying the text.
The index is required: it specifies which element to remove. The index of the first list element is 0, and the index of the last element is Items.Count - 1. If you specify an invalid index, the system throws and IndexOutOfRange exception.
The Items.RemoveAt Method
'Remove the first school in the list lstSchools.Items.RemoveAt(0)
'Remove the items in position IndexInteger.
cboSchools.Items.RemoveAt(IndexInteger) 'Remove the currently selected item. cboSchools.Items.RemoveAt(cboSchools.SelectedIndex)
The Items.Remove Method
The Items.Remove method looks for the specified string in the Items collection. If the string is found, it is removed; however, it it is not found, no exception is generated.
The Items.Remove Method - Examples
'Remove the specified item. lsbSchools.Items.Remove("Gulf Coast High School") 'Remove the matching item. cboSchools.Items.Remove(txtSchool.Text) 'Remove the currently selected item. cboSchools.Items.Remove(CboSchools.Text)
When you remove the currently selected item using either the RemoveAt or Remove method, make your code more efficient and easier to read by using the With statement.
With cbSchools If .SelectedIndex <> -1 Then .Items.RemoveAt(.SelectedIndex) 'Remove by Index or use .Items.Remove(.Text) where text if the item name End if End With
Clearing the List
In addition to removing individual items at run time, you also can clear all items from a list. Use the Items.Clear method to empty a combo box or list box.
The Clear Method - Examples
lsbSchools.Items.Clear() or cboSchools.Items.Clear()
'Confirm clearing the schools list. Dim Response as DialogResult Response = MessageBox.Show("Clear the schools list?", "Clear the Majors List", MessageBoxButtons.YesNo, MessageBoxIcon.Question) If Response = DialogResult.Yes Then cboSchools.Items.Clear() End If