Solved

listbox item selection

Posted on 2001-06-22
7
371 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:
mnasman 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 22

Expert Comment

by:mnasman
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:mnasman
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

759 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

19 Experts available now in Live!

Get 1:1 Help Now