List Box Control Scenario 1: Adding and Deleting Items

This example shows how you can use the AddItem, RemoveItem, and Clear methods with the ListIndex and ListCount properties to add and remove list entries at run time. The example in Figure 7.36 lets a user type a client's name in a text box, which can be added to the list box if the Add button is clicked. A user can remove a current list item by selecting the item and choosing the Remove button, or by choosing Clear to clear all list entries.

Figure 7.36   A list box using the AddItem, RemoveItem, and Clear methods

The number of clients in the list box is displayed in a label that looks like a text box (BorderStyle is set to 1-Fixed Single). This label is updated every time a client name is added or removed. Because the Sorted property for the list box is set to True, items are added to the list box in alphabetical order.

Create a form with a text box, a list box, three labels, and four command buttons. The following table lists the property settings for the objects in the application.

Object Property Setting
Top text box Name
Text
txtName
(Empty)
Top label Name
Caption
lblName
&Name to add
List box Name
Sorted
lstClient
True
Bottom label Name
Caption
lblClients
# Clients
Number of clients label
(looks like a text box)
Name
Caption
BorderStyle
lblDisplay
(Empty)
1-Fixed Single
First command button Name
Caption
cmdAdd
&Add
Second command button Name
Caption
cmdRemove
&Remove
Third command button Name
Caption
cmdClear
&Clear
Fourth command button Name
Caption
cmdClose
&Close

Events in the List Box Application

Add this code to the cmdAdd_Click event procedure:

Private Sub cmdAdd_Click ()
   lstClient.AddItem txtName.Text   ' Add to list.
   txtName.Text = ""      ' Clear text box.
   txtName.SetFocus
   ' Display number.
   lblDisplay.Caption = lstClient.ListCount
End Sub

Add this code to the cmdRemove_Click event procedure:

Private Sub cmdRemove_Click ()
   Dim Ind As Integer

   Ind = lstClient.ListIndex   ' Get index.
   ' Make sure list item is selected.
   If Ind >= 0 Then
      ' Remove it from list box.
      lstClient.RemoveItem Ind
      ' Display number.
      lblDisplay.Caption = lstClient.ListCount
   Else
      Beep
   End If
   ' Disable button if no entries in list.
   cmdRemove.Enabled = (lstClient.ListIndex <> -1)
End Sub

Add this code to the cmdClear_Click event procedure:

Private Sub cmdClear_Click ()
   ' Empty list box.
   lstClient.Clear
   ' Disable Remove button.
   cmdRemove.Enabled = False
   ' Display number.
   lblDisplay.Caption = lstClient.ListCount
End Sub

Add this code to the cmdClose_Click event procedure:

Private Sub cmdClose_Click ()
   Unload Me
End Sub

Add this code to the lstClient_Click event procedure:

Private Sub lstClient_Click ()
   cmdRemove.Enabled = lstClient.ListIndex <> -1
End Sub

Add this code to the txtName_Change event procedure:

Private Sub txtName_Change ()
' Enable the Add button if at least one character 
' in the name.
cmdAdd.Enabled = (Len(txtName.Text) > 0)
End Sub