Partager via


Selected Property

Specifies whether an item is selected in a ComboBox or ListBox control. Not available at design time and read/write at run time.

[Form.]Control.Selected(nIndex) [= lExpr ]

Return Value

  • nIndex
    Numeric data type. Selected specifies the index of an item in a ComboBox or ListBox.

  • lExpr
    Logical data type. The following table lists the settings for the Selected property.

    Setting

    Description

    True (.T.)

    Item is selected.

    False (.F.)

    Item is not selected. (Default)

Remarks

Applies To: ComboBox Control | ListBox Control

Setting the Selected property on a ListBox control also sets the ListItem property and fires the ProgrammaticChange event.

The Selected property is particularly useful where users can make multiple selections. You can quickly check which items in a list are selected. You can also use this property to select or clear items in a list from within a program. To check whether the third item in a list box is selected, use the following code:

IF MyList.Selected(3)
  WAIT WINDOW "It's selected!"
ELSE
  WAIT WINDOW "It's not!"
ENDIF

Example

The following example creates a list box. An array serves as the source of the items that appear in the list box items, and the name of the array is specified using the RowSource property. The RowSourceType property is set to 5 (array) to specify that an array is the source for the items in the list box.

The MultiSelect property for the list box is set to True (.T.), making it possible for you to make multiple selections from the list box. The item or items you choose in the list box are displayed by using the ListCount, Selected, and List properties to determine the number of items in the list box and the items you chose.

CLEAR

DIMENSION gaMyListArray(10)
FOR gnCount = 1 to 10  && Fill the array with letters
   STORE REPLICATE(CHR(gnCount+64),6) TO gaMyListArray(gnCount)
NEXT   

frmMyForm = CREATEOBJECT('Form')  && Create a form.
frmMyForm.Closable = .f.  && Disable window context menu.

frmMyForm.Move(150,10)  && Move the form.

frmMyForm.AddObject('cmbCommand1','cmdMyCmdBtn')  && Add "Quit" command button.
frmMyForm.AddObject('lstListBox1','lstMyListBox')  && Add ListBox control.

frmMyForm.lstListBox1.RowSourceType = 5  && Specifies an array.
frmMyForm.lstListBox1.RowSource = 'gaMyListArray' && Specifies array source containing list box items.

frmMyForm.cmbCommand1.Visible =.T.  && "Quit" command button visible.
frmMyForm.lstListBox1.Visible =.T.  && List box visible.

frmMyForm.SHOW  && Display form.
READ EVENTS  && Start event processing.

DEFINE CLASS cmdMyCmdBtn AS CommandButton  && Create command button.
   Caption = '\<Quit'  && Assign caption on the command button.
   Cancel = .T.  && Assign default Cancel command button (Esc).
   Left = 125  && Command button column.
   Top = 210  && Command button row.
   Height = 25  && Command button height.

   PROCEDURE Click
      CLEAR EVENTS  && Stop event processing and close form.
      CLEAR  && Clear main Visual FoxPro window.
ENDDEFINE

DEFINE CLASS lstMyListBox AS ListBox  && Create ListBox control.
   Left = 10  && List Box column
   Top = 10  && List Box row
   MultiSelect = .T.  && Allow selecting more than 1 item.

PROCEDURE Click
   ACTIVATE SCREEN
   CLEAR
   ? "Selected items:"
   ? "---------------"
   FOR nCnt = 1 TO ThisForm.lstListBox1.ListCount
      IF ThisForm.lstListBox1.Selected(nCnt)  && Is item selected?
         ? SPACE(5) + ThisForm.lstListBox1.List(nCnt) && Show item.
      ENDIF
   ENDFOR

ENDDEFINE

See Also

Reference

AddItem Method (Visual FoxPro)

Clear Method

ItemBackColor, ItemForeColor Properties

List Property

ListCount Property

ListItemID Property

MultiSelect Property (Visual FoxPro)

NewItemID Property

RemoveItem Method

RemoveListItem Method

SelectedID Property

SelectedItemBackColor Property

SelectedItemForeColor Property

TopItemID Property

Other Resources

Properties (Visual FoxPro)