Solved

listbox item selection

Posted on 2001-06-22
7
377 Views
Last Modified: 2010-04-06
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
0
Comment
Question by:gi7mhl
  • 3
  • 3
7 Comments
 
LVL 22

Accepted Solution

by:
Mohammed Nasman earned 60 total points
ID: 6217569
Hello

  you can select any line of the listbox by set the Selected property for the item to true, but you need to set the multi select property to true, to be able to select more than one item

procedure TForm1.Button1Click(Sender: TObject);
var
  I : Integer;
begin
  for I := 0 to ListBox1.Items.Count -1 do
    ListBox1.Selected[I] := True;
end;


Best regards
Mohammed Nasman
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 6217582
procedure TForm1.Button2Click(Sender: TObject);
var i: Integer;
begin
  ListBox1.MultiSelect := true;
  for i := 0 to ListBox1.Items.Count - 1 do
    ListBox1.Selected[i] := true;
end;
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 6217586
to late  :o(
0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 6217597
Don't be sad Epsylon, you have 10 times more than my points ;)
0
 

Author Comment

by:gi7mhl
ID: 6217708
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'
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 6217848
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.ProcessMessages;
    Sleep(1000);
  end;
end;
0
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 6217875
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
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question