The curselection method, discussed in the preceding section, only tells you what the user has selected. You can also change the selection by using a form of the selection method.
To select a range of items in a Listbox, you can use the "set" form of the selection method, selectionSet. selectionSet takes either a single index or a range. Any items not in the range are not affected. If you use a range, the firstindex must be less than or equal to the lastindex. Here are some examples:
# select everything $lb->selectionSet(0, 'end' ); #select the first item $lb->selectionSet(0);
Even if you have used -selectmode to limit the selection to only one item, you can force more than one item to be selected by using selectionSet(...).
To clear any selections in the Listbox, use the "clear" form of the selection method, selectionClear. Pass in an index or a range or indexes from which to clear the selection. For instance, to remove all the selections in the Listbox, you would do the following:
$lb->selectionClear(0, "end");
Any indexes outside the specified range will not be unselected; this allows you to unselect one item at a time. You can also clear the selection from just one item:
$lb->selectionClear("end");
To test if a specific index is already selected, use the "includes" form of selection, selectionIncludes. Calling selectionIncludes returns 1 if the item at the specified index is selected and 0 if it is not. For instance, to see if the last item in the list is selected:
if ($lb->selectionIncludes('end')) { ... }
Using the "anchor" form of selection, selectionAnchor to set the index "anchor" to the specified index. The "anchor" is used when you are selecting several items within the Listbox with the mouse cursor. The first item you click (without letting up on the mouse button) becomes the "anchor" index. For example, use this to set the "anchor" as the first item in the list:
$lb->selectionAnchor(0);