Solved

listbox item selection

Posted on 2001-06-22
7
372 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

895 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now