• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 223
  • Last Modified:

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?
0
MerlaP83
Asked:
MerlaP83
  • 8
  • 8
1 Solution
 
SteveBayCommented:
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

0
 
SteveBayCommented:
I forgot to free the the sList.
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;
     sList.Free;
end;

Open in new window

0
 
MerlaP83Author Commented:
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?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
SteveBayCommented:
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

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

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

n := Lottning.Items.Add(todays[i])
0
 
SteveBayCommented:
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.
0
 
MerlaP83Author Commented:
Darn, its a TListbox.
0
 
SteveBayCommented:
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

0
 
MerlaP83Author Commented:
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
0
 
SteveBayCommented:
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?
0
 
MerlaP83Author Commented:
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

0
 
SteveBayCommented:
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];
 
0
 
MerlaP83Author Commented:
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.
0
 
MerlaP83Author Commented:
Great help and patience, thanks a lot.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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