Question:
When I try to set the selected property of a ListBox
component, I receive an exception that the Index is out
of bounds. How do I get around this?
Answer:
The selected property should only be used with ListBox
components that have the MultiSelect property set to true.
If you are working with a ListBox that has the MultiSelect
property set to false, use the ItemIndex property to select
the item.
Example:
procedure TForm1.Button1Click(Sender: TObject);
begin
ListBox1.Items.Add('1');
ListBox1.Items.Add('2');
{This will fail on a single selection ListBox}
// ListBox1.Selected[1] := true;
ListBox1.ItemIndex := 1; {This is ok}
end;