Compartir a través de


ListBox Control

Creates a list box that displays a list of items that you can select one or more items from. A list box is similar to a combo box; however, a combo box initially displays a single item. For more information about combo boxes, see ComboBox Control.

For more information about creating ListBox controls, see Form Designer and Using Controls.

ListBox

Remarks

The following table lists properties commonly set at design time.

Property

Description

ColumnCount

Determines the number of columns in the list.

ControlSource

Specifies where to store the value that the user chooses from the list.

MoverBars

Specifies whether to display mover bars to the left of the items in the list so that a user can rearrange the item order.

Multiselect

Specifies whether the user can select more than one item in the list.

RowSource

Specifies the source of the values displayed in the list.

RowSourceType

Specifies the type of the source of the values displayed in the list.

The following table lists methods commonly used.

Method

Description

AddItem

Adds an item to the list when RowSourceType is set to 0.

RemoveItem

Removes an item from the list when RowSourceType is set to 0.

Requery

Updates the list if the values in the source specified by RowSource have changed.

The backslash ("\") is treated as a special character when used in the expression for an item. The following rules apply for this character:

  • You can disable an item in a list box or combo box by adding a single backslash to the beginning of the expression.

  • Any multiple of two backslashes used in the expression is displayed as a single backslash. For example, one or two backslashes used together will display as one, and three or four backslashes together will display as two. The following example code contains a list box item containing a Universal Naming Convention (UNC) path. The code would display in the list box as \\MyServer\MyMachine\MyFolder.

    MyForm.List1.AddItem("\\\\MyServer\\MyMachine\\MyFolder")
    
  • If the expression begins with multiple backslashes, the item is not disabled. If you want to disable an item that begins with multiple backslashes, add a backslash and a close bracket (]) to the beginning of the item. For example, the following would disable the UNC path item in the list box:

    MyForm.List1.AddItem("\]\\\MyServer\\MyMachine\\MyFolder")
    
  • To include a separator line, use a backslash followed by a hyphen as the item to add to the list box. For example, the following code adds a separator line to a list box:

    MyForm.List1.AddItem("\-")
    

You can also use an ActiveX control that adds extra characteristics, such as a CheckBox, to the ListView or TreeView controls.

Example

The following example creates a ListBox control. The source of the items that appear in the list box is an array specified with the RowSourceType and RowSource properties.

The MultiSelect property for the list box is set to true (.T.), allowing you to make multiple selections from the list. The item or items you choose are displayed by using the ListCount, Selected, and List properties (which determine the number of items in the list 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 Control menu box

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

Reference

ListBox Control Properties, Methods, and Events

CREATE CLASS Command

CREATE FORM Command

DEFINE CLASS Command

Other Resources

Objects, Collections, and Classes