gi7mhl
asked on
listbox item selection
How can i select the items in a listbox programmatically. I would like to be able to select the items in a loop starting with the one at the top and going down to the last one and be able to check whether I have reached the last item in the listbox
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
to late :o(
Don't be sad Epsylon, you have 10 times more than my points ;)
ASKER
What I want is to have the selected item to be highlighted, the same way as when the mouse is used to select an item. Also when I use the selected property with an index i get the error 'list index out of bounds'
gi7mhl, I don't know what you are doing but the examples should work.
Or do you want something like this:
procedure TForm1.Button1Click(Sender : TObject);
var i: Integer;
begin
ListBox1.Clear;
ListBox1.Items.Add('Item 1');
ListBox1.Items.Add('Item 2');
ListBox1.Items.Add('Item 3');
ListBox1.Items.Add('Item 4');
ListBox1.Items.Add('Item 5');
ListBox1.MultiSelect := false;
for i := 0 to ListBox1.Items.Count - 1 do
begin
ListBox1.ItemIndex := i;
Label1.Caption := ListBox1.Items[i];
Application.ProcessMessage s;
Sleep(1000);
end;
end;
Or do you want something like this:
procedure TForm1.Button1Click(Sender
var i: Integer;
begin
ListBox1.Clear;
ListBox1.Items.Add('Item 1');
ListBox1.Items.Add('Item 2');
ListBox1.Items.Add('Item 3');
ListBox1.Items.Add('Item 4');
ListBox1.Items.Add('Item 5');
ListBox1.MultiSelect := false;
for i := 0 to ListBox1.Items.Count - 1 do
begin
ListBox1.ItemIndex := i;
Label1.Caption := ListBox1.Items[i];
Application.ProcessMessage
Sleep(1000);
end;
end;
gi7mhl, the error u got 'list index out of bounds' cuz u don't set the mulitselect property to true, I told you to set it, and now you can select any item by
ListBox1.Selected[anyindex ] := True;
check my procedure that I post to you first, but don't forget to change the MultiSelect proprty to true, or you can add it in the code as ListBox1.MultiSelect := True; before the loop
Mohammed
ListBox1.Selected[anyindex
check my procedure that I post to you first, but don't forget to change the MultiSelect proprty to true, or you can add it in the code as ListBox1.MultiSelect := True; before the loop
Mohammed
var i: Integer;
begin
ListBox1.MultiSelect := true;
for i := 0 to ListBox1.Items.Count - 1 do
ListBox1.Selected[i] := true;
end;