Visual Basic.NET 2008 > Windows Controls in VB.NET

Adding, Removing and Accessing Items from the ListBox Control's Item Collection in VB.NET 2008

To manipulate a ListBox control from within your application, you should be able to do the following:

  • Add items to the list
  • Remove items from the list
  • Access individual items in the list

The items in the list are represented by the Items collection. You use the members of the Items collection to access the control's items and to add or remove items. The Items property exposes the standard members of a collection, which are described later in this section.

Each member of the Items collection is an object. In most cases, we use ListBox controls to store strings, but it's possible to store objects. When you add an object to a ListBox control, a string is displayed on the corresponding line of the control. This is the string returned by the object's ToString method. This is the property of the object that will be displayed by default. You can display any other property of the object by setting the control's ValueMember property to the name of the property.

If you add a Color object and a Rectangle object to the Items collection with the following statements:

ListBox1.Items.Add(New Font("Verdana", 12, FontStyle.Bold)
ListBox1.Items.Add(New Rectangle(0, 0, 100, 100))

then the following strings appear on the first two lines of the control:

[Font: Name=Verdana, Size=12, Units=3, GdiCharSet=1, gdiVerticalFont=False]
{X=0, Y=0, Width=100, Height=100}

However, you can access the members of the two objects because the ListBox stores objects, not their descriptions. The following statement prints the width of the Rectangle object (the output produced by the statement is highlighted):


The expression in the preceding statement is late-bound, which means that the compiler doesn't know whether the first object in the Items collection is a Rectangle object and it can't verify the member Width. If you attempt to call the Width property of the first item in the collection, you'll get an exception at runtime indicating that the code has attempted to access a missing member. The missing member is the Width property of the Font object.

The proper way to read the objects stored in a ListBox control is to examine the type of the object first and then attempt to retrieve a property (or call a method) of the object, only if it's of the appropriate type. Here's how you would read the Width property of a Rectangle object:

If ListBox1.Items.Item(0).GetType Is GetType(Rectangle) Then
Debug.WriteLine(CType(ListBox1.Items.Item(0), Rectangle).Width)
End If

The Add Method of ListBox Control

To add items to the list, use the Items.Add or Items.Insert method. The syntax of the Add method is as follows:


The item parameter is the object to be added to the list. You can add any object to the ListBox control, but items are usually strings. The Add method appends new items to the end of the list, unless the Sorted property has been set to True.

The following loop adds the elements of the array words to a ListBox control, one at a time:

Dim words(100) As String
{ statements to populate array }
Dim i As Integer
For i = 0 To 99

Similarly, you can iterate through all the items on the control by using a loop such as the following:

Dim i As Integer
For i = 0 To ListBox1.Items.Count - 1
{ statements to process item ListBox1.Items(i) }

You can also use the For Each . . . Next statement to iterate through the Items collection, as shown here:

Dim itm As Object
For Each itm In ListBox1.Items
{ process the current item, represented by the itm variable }

When you populate a ListBox control with a large number of items, call the BeginUpdate method before starting the loop and call the EndUpdate method when you're done. These two methods turn off the visual update of the control while you're populating it and they speed up the process considerably. When the EndUpdate method is called, the control is redrawn with all the items.

The Insert Method of ListBox Control

To insert an item at a specific location, use the Insert method, whose syntax is as follows:

ListBox1.Items.Insert(index, item)

The item parameter is the object to be added, and index is the location of the new item. The first item's index in the list is zero. Note that you need not insert items at specific locations when the list is sorted. If you do, the items will be inserted at the specified locations, but the list will no longer be sorted.

The Clear Method of ListBox Control

The Clear method removes all the items from the control. Its syntax is quite simple:


The Count Property of ListBox Control

This is the number of items in the list. If you want to access all the items with a For . . . Next loop, the loop's counter must go from 0 to ListBox.Items.Count - 1, as shown in the example of the Add method.

The CopyTo Method of ListBox Control

The CopyTo method of the Items collection retrieves all the items from a ListBox control and stores them in the array passed to the method as an argument. The syntax of the CopyTo method is

ListBox.CopyTo(destination, index)

where destination is the name of the array that will accept the items, and index is the index of an element in the array where the first item will be stored. The array that will hold the items of the control must be declared explicitly and must be large enough to hold all the items.

The Remove and RemoveAt Methods of ListBox Control

To remove an item from the list, you can simply call the Items collection's Remove method, passing the object to be removed as an argument. If the control contains strings, pass the string to be removed. If the same string appears multiple times on the control, only the first instance will be removed.

You can also remove an item by specifying its position in the list via the RemoveAt method, which accepts as argument the position of the item to be removed:


The index parameter is the order of the item to be removed, and the first item's order is 0.

The Contains Method of ListBox Control

The Contains method of the Items collection — not to be confused with the control's Contains method — accepts an object as an argument and returns a True/False value that indicates whether the collection contains this object. Use the Contains method to avoid the insertion of identical objects into the ListBox control. The following statements add a string to the Items collection, only if the string isn't already part of the collection:

Dim itm As String = "Remote Computing"
If Not ListBox1.Items.Contains(itm) Then
End If

More lessons on VB.NET ListBox control

Table of Contents Copyright 2011 All Rights Reserved
  Home | Useful links | Contact us | Privacy Policy