Selected Property

Applies To

List Box Control.

Description

You can use the Selected property in Visual Basic to determine if an item in a list box is selected.

Setting

The Selected property is a zero based array of all the items in a list box that contains the selected state of each item in the list.

Setting

Description

True (-1)

The list box item is selected.

False (0)

The list box item isn’t selected.


You can get or set the Selected property using Visual Basic.

This property is available only at run time.

Remarks

When a list box control’s MultiSelect property is set to None, only one item can have its Selected property set to True. When a list box control’s MultiSelect Property is set to Simple or Extended, any or all of the items can have their Selected property set to True.

You can use the Selected property to select items in a list box using Visual Basic. For example, the following expression selects the fifth item in the list.


Me!Listbox.Selected(4) = True

See Also

Column Property, ItemsSelected Collection, ListIndex Property, MultiSelect Property.

Example

The following example uses the Selected property to move selected items in the lstSource list box to the lstDestination list box. The lstDestination list box’s RowSourceType property is set to Value List and the control’s RowSource property is constructed from all the selected items in the lstSource control. The lstSource list box’s MultiSelect property is set to Extended. The CopySelected( ) function is called from the cmdCopyItems command button.


Sub cmdCopyItem_Click()
    CopySelected MeSub
CopySelected(frm As Form) As Integer
    Dim ctlSource As Control
    Dim ctlDest As Control
    Dim strItems As String
    Dim intCurrentRow As Integer
    Set ctlSource = frm!lstSource
    Set ctlDest = frm!lstDestination
    For intCurrentRow = 0 To ctlSource.Listcount - 1
        If ctlSource.Selected(intCurrentRow) = True Then
            strItems = strItems & ctlSource.Column(0, intCurrentRow) _
                & ";"
        End If
    Next intCurrentRow
    ' Reset destination control's RowSource.
    ctlDest.RowSource = ""
    ctlDest.RowSource = strItemsFunction