[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 426
  • Last Modified:

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?
0
Softtech
Asked:
Softtech
  • 2
1 Solution
 
owCommented:
Hi softtech,

you may use a seperate stringlist first, where you can insert strings at any positon.
Then deliver it to the listbox:

var
  StringList :tStringlist;
  Listbox1 :tListBox;
.
StringList.Insert(0, '1');
StringList.Insert(0, '2');
StringList.Insert(0, '3');
.
ListBox1.Items := StringList;

regards
  ow
0
 
borupCommented:
Nice solution ;-)
0
 
SofttechAuthor Commented:
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)

???
0
 
owCommented:
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
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now