Saturday, December 26, 2009

List Box

LISTBOXES

Of all the standard controls, the ListBox and ComboBox are best suited for categorizing and listing information from which users can make choices and selections. These controls are closely related, and the manner in which you manipulate them is almost identical. The difference is that the ComboBox combines a ListBox control with a TextBox (hence the name) and allows users to select from a list or type a new value.

Both the ListBox and ComboBox controls can handle lists. In Visual Basic, a list is an array of strings formally referred to with the List property. (The List property is common to the ListBox and ComboBox controls.) Most work that you do with ListBoxes and ComboBoxes involves adding and removing strings from the List property. You can add strings to the List property of a ListBox or ComboBox at design time or runtime.

Add strings to a ListBox or ComboBox at design time
1. Add a ListBox or a ComboBox control to the form.

2. Select this control on the form.

3. In the Properties window, select the List property and type the strings for the List property in the drop-down box. To add multiple lines to the List property, press Ctrl+Enter to
add a new line to it

At design time, the strings you added to the List property will appear in the ListBox on the form .


This design-time method for populating the List property is useful when your program starts up. Because the contents of a ListBox or ComboBox can change frequently while your program is running, however, you need to be able to add and remove items to and from the List property while it's running. To do this, the ListBox and CombBox controls make extensive use of the AddItem, RemoveItem, and Clear methods.

To add a string to the List of a Listbox or ComboBox during runtime, use the AddItem method, which has the following syntax:

Object.AddItem StringToAdd

In this syntax,

Object is the Name property of the ListBox or ComboBox.

AddItem is the Visual Basic keyword for the method.

StringToAdd is the string that you want to add to the control's list.

Listing 3.13 shows you how to use the AddItem method in a form's Load event procedure to add strings to the List of a ListBox.

LISTING 3.13 11LIST04.TXT--Using the AddItem Method to Add Items to a ListBox Control

01 Private Sub Form_Load()
02 lstHero.AddItem "Superman"
03 lstHero.AddItem "Batman"
04 lstHero.AddItem "Green Lantern"
05 lstHero.AddItem "Aquaman"
06 lstHero.AddItem "SpiderMan"
07 lstHero.AddItem "Daredevil"
08 lstHero.AddItem "Hulk"
09 End Sub

Selecting Items from a List

To understand how Visual Basic determines the value of a string selected in the List of a ListBox or ComboBox, you need to understand that a List is an array of strings. As you learned earlier in this chapter, an array is declared as follows:

Dim ArrayName(Subscript) As DataType

Therefore, if you declared a four-element array as MyArray(3), you would list the elements in that array as follows:

MyArray(0)
MyArray(1)
MyArray(2)
MyArray(3)

If you want to determine the value assigned to the second element, you could use the following statement (remember, by default the first element is MyArray(0)):

MyValue = MyArray(1)

The ListBox and ComboBox controls use a similar format. The property List is the general term that Visual Basic uses to refer to the entire array of strings within a ListBox or ComboBox. Therefore, to find
the value of the second string in the ListBox called lstHero (from Listing 11.4), you would use the following statement:

SecondString$ = lstHero.List(1)

The value of SecondString$ is "Batman".

For a ListBox or ComboBox, the selected item in a list is contained in the ListIndex property. When you select a string in a ListBox, Visual Basic assigns the position number of that string to the ListIndex
property of the control. Therefore, to determine the value of the selected string in lstHero (from Listing 3.4), you would use the following code:

Private Sub lstHero_Click()
lblHero.Caption = lstHero.List(lstHero.ListIndex)
End Sub

This is the event procedure for the Click event of the lstHero ListBox control. When the user clicks a string in the ListBox, the code executes and reports back the value of the selection within the Caption property of the Label named lblHero. Figure 11.10 shows what happens when the user selects a string.


A faster way to find out the value of the string a user selects in a ListBox or ComboBox is to use the Text property. For example, you can use the following for a ListBox:

Dim strMyStr as String
strMyStr = List1.Text

For a ComboBox, use the following:

Dim strMyStr as String
strMyStr = Combo1.Text

Removing Items from a List


You remove a string from a list in a ListBox or ComboBox by using the RemoveItem method:

Object.RemoveItem Index

In this syntax

Object is the Name property of the ListBox or ComboBox.

RemoveItem is the Visual Basic keyword for the method used to remove items from a list.

Index is the position in the List property of the string that you want to remove from the control. To remove a selected item from a list, use the ListIndex property of the control.

Listing 3.14 shows the RemoveItem method used in code.


LISTING 3.14 LIST05.TXT--Using the RemoveItem Method to Remove a String
from a ListBox

01 Private Sub cmdRemove_Click()
02 lstHero.RemoveItem (lstHero.ListIndex)
03 lblHero.Caption = ""
04 End Sub

When you remove an item from a ListBox or ComboBox, make sure that you clear the text from the Caption property of the Label control lblHero, as shown in line 3 of Listing 3.14. If you don't do this, the user will remove the string from the ListBox, but it will remain in the Label control.


Clearing a List


If you want to remove all the strings contained in a ListBox or ComboBox, use the Clear method:

Object.Clear

In this syntax

Object is the Name property of the ListBox or ComboBox.

Clear is the Visual Basic keyword for the method that removes all items from a list.

lstHero.Clear

No comments:

Post a Comment