Link to home
Start Free TrialLog in
Avatar of MerlaP83
MerlaP83

asked on

Listbox / .txt problem

Hey all,

I have a .txt file which contains from 10-50 lines. What I would like is for them to be loaded into a TListbox called 'Lottning'.
The problem however is that I want them to be added like this.

Row 1 - Row 2
Row 3 - Row 4
Row 5 - Row 6
etc..

And as I never know how many rows there are, I can't figure out how to make it work?
Avatar of SteveBay
SteveBay
Flag of United States of America image

Try This:
procedure TForm1.Button1Click(Sender: TObject);
var sList : TStringList;
     i, n : Integer;
begin
     sList := TStringList.Create;
     sList.LoadFromFile( 'MyFile.txt');
     Lottning.Clear;
     for i := 0 to sList.Count -1 do
          begin
          if (i mod 2) = 0 then
               n := Lottning.Items.Add(sList[i])
          else
               Lottning.Items[n] := Lottning.Items[n] + ' - '  + sList[i];
          end;
end;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of SteveBay
SteveBay
Flag of United States of America image

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
Avatar of MerlaP83
MerlaP83

ASKER

Excellent work. Thanks a lot.

Just another question before I hand out the points.

If I want to achieve the same thing, but the lines are in a TStringList called "todays" instead of in a .txt file? What should I change then?
That's even more simple:
procedure TForm1.Button1Click(Sender: TObject);
var i, n : Integer;
begin
     Lottning.Clear;
     for i := 0 to todays.Count -1 do
          begin
          if (i mod 2) = 0 then
               n := Lottning.Items.Add(todays[i])
          else
               Lottning.Items[n] := Lottning.Items[n] + ' - '  + todays[i];
          end;
end;

Open in new window

Thanks, but I get an error saying "Class does not have a default property." ?
Which line gives you an error?
Sorry.

This line gives me the error, its the "todays[i]" which is causing it.

n := Lottning.Items.Add(todays[i])
Are you sure that todays is a TStringList? Because a TStringList does have a default value which is Strings[i]. If it is not a TStringList you may find that Todays.Strings[i] returns a value.
Darn, its a TListbox.
then it looks like this:
procedure TForm1.Button1Click(Sender: TObject);
var i, n : Integer;
begin
     Lottning.Clear;
     for i := 0 to todays.Items.Count -1 do
          begin
          if (i mod 2) = 0 then
               n := Lottning.Items.Add(todays.Items[i])
          else
               Lottning.Items[n] := Lottning.Items[n] + ' - '  + todays.Items[i];
          end;
end;

Open in new window

Thanks, no errors now. But for some reason it doesn't end up as it did with the TStringList ?

Any ideas why?

Attached a file. They are not added in the correct way and I have no idea why.
problem.jpg
It works fine for me.
Are you using the exact code I posted? Perhaps it would help If I could see your code.
Is Todays a TListBox or is it perhaps a TDBListBox?
That is weird.

I have renamed Lottning to Main_Dagens which is a TListbox
and I have renamed todays to sch_todaysgames which is also
a TListbox.
var i, n : Integer;
begin
main_dagens.clear;
     for i := 0 to sch_TodaysGames.Count -1 do
          begin
          if (i mod 2) = 0 then
               n := Main_Dagens.Items.Add(sch_TodaysGames.items.Strings[i])
          else
               Main_Dagens.Items[n] := sch_TodaysGames.Items[n] + ' - '  + sch_TodaysGames.items.strings[i];
          end;

Open in new window

Change this:
   Main_Dagens.Items[n] := sch_TodaysGames.Items[n] + ' - '  + sch_TodaysGames.items.strings[i];
          end;
 to this:
    Main_Dagens.Items[n] := Main_Dagens.Items[n] + ' - '  + sch_TodaysGames.items.strings[i];
 
Heh, that did the trick.. must have renamed it wrong previously.

Anyway, thanks a bunch for all your help! Everything working just the way I wanted.
Great help and patience, thanks a lot.