Link to home
Start Free TrialLog in
Avatar of Softtech
Softtech

asked on

Add items to TListBox in REVERSE order

I know that to add an item to a TListBox, one simply uses...

ListBox1.Items.Add(Edit1.Text);

However, this adds the new item to the END of the list.  What I am trying to accomplish is to place the new item at the BEGINNING of the list.

IOW, I would like to see this...

ListBox1.Items.Add('1');
ListBox1.Items.Add('2');
ListBox1.Items.Add('3');

...result in the following display in the ListBox...

3
2
1

...instead of the current behavior, which gives me...

1
2
3

What is the best way to accomplish this reverse order?
ASKER CERTIFIED SOLUTION
Avatar of ow
ow

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Nice solution ;-)
Avatar of Softtech
Softtech

ASKER

I still have to .Create and .Free the TStringList, don't I?

Out of curiousity, if I wanted to have take the contents of the StringList, and have it fill the TListBox, would I simply do this?...

MyStringList := TStringList.Create;
.
.
ListBox1.Items.Assign(MyStringList)

???
Softtech,

you are right at all.
You must create and free the stringlist and you can use either "ListBox.Items.Assign(StringList)" or "ListBox.Items := StringList".
Items.Assign is just the method, which is called, when you write to property tListbox.Items.

regards
  ow