共用方式為


Selected Property

Specifies whether an item is selected in a combo box or list box. Not available at design time; read-write at run time.

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

Property Values

  • nIndex
    Specifies the index of an item in a combo box or list box.
  • lExpr
    The settings for the Selected property are:
    Setting Description
    True (.T.) The item is selected.
    False (.F.) (Default) The item isn't selected.

Remarks

Setting the Selected property on a Listbox control also sets the ListItem property, and fires the Programmatic Change 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 deselect items in a list from within a program. To check whether the third item in a list box is selected, issue the following:

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

Example

The following example creates a list box. The source of the items that appear in the list box items is an array; the name of the array is specified with 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 the window pop-up 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' && Array containing listbox items

frmMyForm.cmbCommand1.Visible =.T.  && "Quit" Command button visible
frmMyForm.lstListBox1.Visible =.T.  && "List Box visible

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

DEFINE CLASS cmdMyCmdBtn AS CommandButton  && Create Command button
   Caption = '\<Quit'  && Caption on the Command button
   Cancel = .T.  && 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, 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

AddItem Method | Clear Method | List Property | ListCount Property | ListItemID Property | MultiSelect Property | NewItemID Property | RemoveItem Method | RemoveListItem Method | SelectedID Property | TopItemID Property

Applies To: ComboBox | ListBox