Solved

listbox item selection

Posted on 2001-06-22
7
375 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Simple Delphi Question 9 89
Dev express lookupcombo 3 27
RESTRequest Parameter 4 30
Twebbrowser in Fire monkey android scrolling to bottom 1 30
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

770 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