Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

listbox item selection

Posted on 2001-06-22
7
Medium Priority
?
393 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 22

Accepted Solution

by:
Mohammed Nasman earned 180 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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 I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

610 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