FoxPro multiselect listbox

The standard FoxPro listbox control allows the user to easily select an item from the list that is displayed. If you set the multiselect property of the listbox then you can let the user make multiple selections from the list. These selections follow the usual conventions of the Windows user interface:

  • Click on a row to select or deselect a single item.
  • Hold down CTRL and click to add an item to a selection.
  • Hold down CTRL and click on a selected item to remove it from a selection
  • Hold down SHIFT and click to extend a selection

Multiselect listbox

Coding for multiple selections

The first step is to set the MultiSelect property of the listbox to .T.. This enables the behaviour described above. When a row is selected a value of .T. is stored in the equivalent entry in the control's Selected property. This property is an array with one element for every row in the listbox. Scan this array to determine which rows have been selected.

In the example above, the InteractiveChange event of the listbox calls the following code to create the list of selected items:

With Thisform
   .edtSelected.Value = ""
   For ln = 1 To .lstMulti.ListCount
     If .lstMulti.Selected(ln)
       *-- Add this to the editbox
       .edtSelected.Value = .edtSelected.Value + ;
           .lstMulti.List(ln) + Chr(13)
     Endif
   Next ln
EndWith

Similar loops behind the Clear all and Select all buttons set all elements of the array to .T. and .F. respectively.

Other listbox properties

By default the selected items will appear in the standard colours from the user's Windows colour scheme. You can override these with the SelectedItemBackColor and SelectedItemForeColor properties but remember that the user may have taken great care in chosing their own colour scheme to suit their own requirements. To take an extreme example, a user with some form of red/green colour blindness would not be pleased if you ignored their own preferences and displayed selected items in red and green.

Alternatives

The listbox with its multiselect property is an easy way of letting your user make multiple selections. Consider using a grid with checkboxes if you need a more sophisticated interface:

A grid with multiple selections

Back to FoxPro Developers' page


Hints & tips

The textbox class in Visual FoxPro 9 has a new Autocomplete property which shows the user the previous values that have been entered in that textbox.
Autocomplete in VFP 9

Your Access database will look more impressive if you add custom toolbars...
Custom toolbars

FoxPro has always had functions to read and write files at a low level...
Foxpro low level file functions

More...
More pages of hints and tips for users of Microsoft FoxPro and Access databases.

Site Map