FoxPro multiselect listbox

Multiselect listbox The standard listbox control in Visual FoxPro 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

Coding for multiple selections

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

In the example below, 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

A Visual FoxPro grid with multiple selections The listbox with its multiselect property is an easy way of letting your user make multiple selections but not all users know of the Windows conventions for selecting several items. Consider using a grid with checkboxes if you need a more sophisticated interface. This technique also allows you to add a very useful pair of "Tick All" and "Clear All" buttons.