# Loop a listbox

hello there.

I have a listbox, with 5 items

Apple
Pear
Grape
Orange
Peach

What I need to do is something like this

For i := 0 to 99 do
memo1.lines.add(Listbox1.items[i];

Now when i = 5 it is going to error, so it should do this
Apple
Pear
Grape
Orange
Peach
Apple1
Pear1
Grape1
Orange1
Peach1
Apple2
Pear2
Grape2
Orange2
Peach2

Etc
So it loops the list and adds a number to the end. Something so simple and I can't figure it out.

Thanks alot

LVL 10
Asked:
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Try this (Not too elegent but works)

//-----------------------------------------------------------------------------
procedure TForm1.Button1Click(Sender: TObject);
var
i, j, k: Integer;
begin
k := 0;
for i := 0 to 99 do
begin
for j := 0 to Pred(ListBox1.Items.Count) do
begin
if k = 0 then
begin
memo1.Lines.Add(ListBox1.Items.Strings[j]);
end
else
begin
Memo1.Lines.Add(ListBox1.Items.Strings[j] + IntToStr(k));
end;
if j = Pred(ListBox1.Items.Count) then
begin
inc(k,1);
end;
end;
end;
end;
//-----------------------------------------------------------------------------

Cheers.
Colin.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
Here's another one  just nutted out :)

procedure TForm1.Button1Click(Sender: TObject);
var
loops, i, item : integer;
Begin
For i := 0 to 99 do
begin
If (i <= listbox1.items.Count-1) or (i = 0) then
memo1.lines.add(listbox1.Items[i])
else
begin
loops := i div listbox1.items.Count;
item := i-(loops*listbox1.items.Count);
memo1.lines.add( listbox1.Items[item] + IntToStr(loops) );
end;
end;
end;
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.