Database development and training for Birmingham and the midlands
Specialists in Microsoft Access and Visual Foxpro database training and development
I am happy to hear from existing clients but I am taking no new calls.
FoxPro 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
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:
.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
and 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
and 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 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.