Solved

Add items to TListBox in REVERSE order

Posted on 1998-10-09
4
366 Views
Last Modified: 2011-09-20
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
Comment
Question by:Softtech
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
ow earned 80 total points
Comment Utility
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
 
LVL 1

Expert Comment

by:borup
Comment Utility
Nice solution ;-)
0
 

Author Comment

by:Softtech
Comment Utility
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
 
LVL 1

Expert Comment

by:ow
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now