ListBox Control

Description

Displays a list of values and lets you select one or more.

Remarks

If the ListBox is bound to a data source, then the ListBox stores the selected value in that data source.

The ListBox can either appear as a list or as a group of OptionButton controls or CheckBox controls.

The default property for a ListBox is the Value property.

The default event for a ListBox is the Click event.

Note You can't drop text into a drop-down ListBox.

Properties

BackColor property, BorderColor property, BorderStyle property, BoundColumn property, BoundValue property, Column property, ColumnCount property, ColumnHeads property, ColumnWidths property, ControlSource property, ControlTipText property, Enabled property, Font object, ForeColor property, Height, Width properties, HelpContextID property, IMEMode property, IntegralHeight property, LayoutEffect property, Left, Top properties, List property, ListCount property, ListIndex property, ListStyle property, Locked property, MatchEntry property, MouseIcon property, MousePointer property, MultiSelect property, Name property, Object property, OldHeight, OldWidth properties, OldLeft, OldTop properties, Parent property, RowSource property, Selected property, SpecialEffect property, TabIndex property, TabStop property, Tag property, Text property, TextColumn property, TopIndex property, Value property, Visible property.

Methods

AddItem method, Clear method, Move method, RemoveItem method, SetFocus method, ZOrder method.

Events

AfterUpdate event, BeforeDragOver event, BeforeDropOrPaste event, BeforeUpdate event, Change event, Click event, DblClick event, Enter, Exit events, Error event, KeyDown, KeyUp events, KeyPress event, MouseDown, MouseUp events, MouseMove event.

See Also

CheckBox control, ComboBox control, OptionButton 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 Declarations portion of a form. Make sure that the form contains:

  • A ListBox named ListBox1.
  • Two CommandButton controls named CommandButton1 and CommandButton2.
    Dim EntryCount As Single
    Private Sub CommandButton1_Click()
        EntryCount = EntryCount + 1
        ListBox1.AddItem (EntryCount & " - Selection")
    End Sub
    
    Private 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
    
    Private Sub UserForm_Initialize()
        EntryCount = 0
        CommandButton1.Caption = "Add Item"
        CommandButton2.Caption = "Remove Item"
    End Sub