Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Listbox, select item by code?

Status
Not open for further replies.

Ante0

Programmer
Apr 19, 2007
98
SE
Is there any way to select an item in a listbox by code?
I have this playlist for an mp3 player that I've made, but when a song is playing it doesn't select the item in the listbox. If I for example doubleclick on any item in the list, it plays in the mediaplayer component, but when it changes to the next song I want it to select that item, and not stay at the one I clicked on to begin with.
Is this possible, if so, post any examples/snippets that would help. I've been looking for this for a long time :)
 
easy:

Code:
 ...
 // select first item in our listbox
 Listbox1.Selected[0]:= True;
 ...

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Here's a question:

I was going to suggest that, but when I tested it, I got an exception EListError "List Index out of bounds (0)". Why? (I added two entries to the listbox before I tried the code above).
 
mmm weird, I just dropped a listbox onto a form, added 5 entries and tried the piece of code. it should work!

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
First you have to enter some strings for it to work, otherwise there's nothing in the list to be selected.
You could
Thanks whosrdaddy, heh, I tried with Listbox1.Selected[0}; first... No wonder it didn't work, it was not set to True :)
Now I need to make a new thread, again, this is the best forum for delphi questions. And googling only gets you to VCL sites, no snippets or other forums.
 
First you have to enter some strings for it to work, otherwise there's nothing in the list to be selected.
You could put
Code:
procedure TForm1.Button1Click(Sender: TObject);
begin
if ListBox1.Items.Count > 0 then
begin
Listbox1.Selected[0]:= True;
end;
end;
That would take care of (List out of bounds(0))
Thanks whosrdaddy, heh, I tried with Listbox1.Selected[0}; first... No wonder it didn't work, it was not set to True :)
Now I need to make a new thread, again, this is the best forum for delphi questions. And googling only gets you to VCL sites, no snippets or other forums.
 
Actually I figured out why the code didn't work as expected: MultiSelect must be true in the Listbox (at least on my implementation of Delphi) or any reference to "Selected" as an array will fail.
 
Glenn,

what delphi version do you have? (D2006 here)
my code works with multiselect=false

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Delphi 3 here. My best guess is that the array is readonly since I can set up code to find out what is selected simply enough.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top