The ListBox Control’s’ Methods VB Help

To manipulate a listBox control from within your should be able t?:

• Add items to the list
• Remove items from the list
• Accessindividual items in the list

Add Item

To add items to the list, use the Addltem method. Its syntax is as follows: List .Addltem item, index

The item parameter is the string to be added to the list, and index is its order. The first item’s order in the list is zero. The index argument is optional; if you omit it, ·the string is appended to the bottom of the list. If the control’s Sorted property is set to True, the item is inserted in its proper place in the list, regardless of the value of the index argument.

Remove ltem

To remove an item, from the list, you must first find its position (index) in the list, which you must then supply to .the Removeltem method. The syntax of the method is as follows:

Listl.Removeltem index

The index parameter is the order of the item to be removed, and tJ;ristime, it’s not optional. The following statement removes the item at the top of the list: Listl.Removeltem 0


The Clear method removes all the items from the control. Its syntax is simple: List. (1 year) To access individual items, you can use several properties that are unique to the ListBox (and ComboBox) control. Removing an item from a list requires that you know its order in the list, but the Remo’veItem method’s argument is rarely used. Usually, you rely on the user to select the items to be removed; otherwise, the items will be selected from within your code based on their values. The Listbox control provides several properties that let you read the items from within your code and they’re described next.


This is the number of items in the List. The items in the list can be accessed withan Index value, which goes from D to ListCount – 1.ListO  This is an array that holds the list’s items. The element List(D)holds the first element of thelist, List(l) holds the second item, and so on up to List(ListCount-l), which holds the last item. The List() array is used frequently in scanning all the List items. The following loop, for instance, scans all the items of the Listl control, looking for blank strings to remove:

Notice that the loop ‘scans the elements of the ListO array backward. Can you see why? If the list was scanned forward, each time an item was removed, the list’s length would decrease by 1: This would cause the loop to be executed more times than there are ite..•ns in the List, resulting in a runtime error. Scanning the items backwards prevents this.

List lndex

This is the index of the selected item in the List. Ifmultiple items are selected, ListIridex is,the index of the most recently selected item. Use as property to access specific elements in the list or delete specific items. The)following statementremoves the selected item from the Listl control, but only if an item  is selected:

If no item is selected at the time the RemoveItem method is invoked, the ListIndex property has a negative value; any attempt to remove an item with a negative index results in a runtime error, To avoid this error, check the value of the Listlndex property first:

Afterthe removal of the item, the indices of the following items are adjusted accordingly,


This property is an array, similar to the List property, with elements that } have a True or a False value, depending-on the status of the corresponding the value is True if the element is selected; otherwise; it’s False}. Because there is .no property equivalent to the ListIndex property for multiple selected items, the only way to find out which elements have been selected is to examine all the elements of the Selected array (you’ll see an example of this technique shortly).


This property reports the number of selected items in a ListBox control with its MultiSelect property set to 1 (Simple) or 2 (Extended). It’s’ commonly used in control junction with the Selected,array in processing the selected items in the control. If a’ ListBox control allows muaiple dements [0 be selected, you might want to use the checkbox style for yom list’s items. Set the Style property to 1 so that the list’s ‘. items will ~played as checkboxes, like the ones shown in Figure 5.6, earlier in this chapter.


This property returns the irdex of the item most recently added to a ListBox control. This property is used .ornmonly with the J temData property, which will be discussed shortly in the section “Indexing ,. ith the List Control.” VB6 at Work:

The ListDemo Project

The ListDemo application (shown in Figure 5.7)demonstrates the basic operations of the ListBox control.The two LbtBox controls on the Form operate slightly different. The ~t one has the defa ult con fj gu ration: only one item can be selected at a time, and new items are appended after the existing item. The second ListBox control has its Sorted property set to 1rue and .ts Multibelect properly set to 2 . (Extended): This means that the elements of this control are always sorted, and the user can select multiple cells with the mouse, wh ile h. liding down either the Shift or the Control keys.
The code for-the ListDemo application contain all the logic you’ll need in your ListBox manipulation routlm-s. lt shows YOU how.

.• Add and remove items
• Transfer items between lists
• Handle multiple selected items
• Maintain sorted lists

Add.New Element B~tton .The Add New l lement bt.ttons use the InputBox() function to prompt the user for input, and then they’ ad.I the r.ser- supplied string to the ListBox control. The code is identical for both buttons, an- f it’s shown next.

Add New Element Buttons

Notice that the subroutine examines the data entered by the user to avoid adding. blank strings to the list. TIle code for the Clear List button is also straightforward; it simply calls the Clear method to remove all entries from the corresponding list. Remove Selected Item(s) Button The code for the Remove Selected Item , button is different from that for the Remove Selected Items button. The reason is that the first Listbox can have only one selected item and the second can have multiple selected items. To delete an item, you must have at least one item selected (explained earlier),

Posted on November 4, 2015 in Basic ActiveX Controls

Share the Story

Back to Top
Share This