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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Concat items in listbox 1

Status
Not open for further replies.

rincewind666

Programmer
Nov 1, 2003
2
GB
I need to concat all the lines in a listbox (except the last line) into a string. Something like:

First line
Second line
Third line
Fourth line

into "First line Second line Third line"

The problem is that the number of lines in the listbox would vary and I do not know beforehand how many there are.

Any help would be greatly appreciated.
 
with ListBox1 do
for i:=0 to Count-2 do
ResultString:=ResultString+Items;
 
Try this.
Assume that your listbox has these items:

First line
Second line
Third line
Fourth line

If you want to know the number of lines in a listbox,
ListBox1.Items.Count; //returns 4.

If you want to get a string by line, use by index.
ListBox1.Items.Strings[0]; //returns string 'First line'
ListBox1.Items.Strings[1]; //returns string 'Second line'
... and so on ...
ListBox1.Items.Strings[3]; //returns string 'Fourth line'

Take note. Indexing starts always from 0.
If ListBox1.Items.Count returns 4 and you want to get the last line in a listbox, the code might look like this.

with ListBox1.Items do
LastLineString := Strings[Count-1];


Finally your want. Add 1 button.
On click event write this code;

procedure TForm1.Button1Click(Sender: TObject);
var
Ctr : Integer;
St : String;
begin
St := '';
with ListBox1.Items do
for Ctr := 0 to Count - 2 do
St := St + ' ' + Strings[Ctr];
ShowMessage(St);
end;

Take note. I used "Count - 2" inorder to skip the last line.

FITZ
email: LANThel@go.com

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top