ListBox Control Example

The following example adds and deletes the contents of a ListBox using the AddItem, RemoveItem, and SetFocus methods, and the ListIndex and ListCount properties.

To use this example, copy this sample code to the Script Editor of a form. To run the code you need to open the form so the Open event will activate. Make sure that the form contains:

Dim EntryCount 
Dim Listbox1

Sub Item_Open()
  Set ListBox1 = Item.GetInspector.ModifiedFormPages("P.2").ListBox1
  Set CommandButton1 = Item.GetInspector.ModifiedFormPages("P.2").CommandButton1
  Set CommandButton2 = Item.GetInspector.ModifiedFormPages("P.2").CommandButton2
  EntryCount = 0
  CommandButton1.Caption = "Add Item"
  CommandButton2.Caption = "Remove Item"
End Sub

Sub CommandButton1_Click()
  EntryCount = EntryCount + 1
  ListBox1.AddItem (EntryCount & " - Selection")
End Sub
    

Sub CommandButton2_Click()
  ListBox1.SetFocus

  'Ensure ListBox contains list items
  If ListBox1.ListCount >= 1 Then
      'If no selection, choose last list item.
      If ListBox1.ListIndex = -1 Then
          ListBox1.ListIndex = ListBox1.ListCount - 1
      End If
      ListBox1.RemoveItem (ListBox1.ListIndex)
  End If
End Sub