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?
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?
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;
ASKER
Thanks, but I get an error saying "Class does not have a default property." ?
Which line gives you an error?
ASKER
Sorry.
This line gives me the error, its the "todays[i]" which is causing it.
n := Lottning.Items.Add(todays[ i])
This line gives me the error, its the "todays[i]" which is causing it.
n := Lottning.Items.Add(todays[
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.
ASKER
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;
ASKER
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
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?
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?
ASKER
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.
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;
Change this:
Main_Dagens.Items[n] := sch_TodaysGames.Items[n] + ' - ' + sch_TodaysGames.items.stri ngs[i];
end;
to this:
Main_Dagens.Items[n] := Main_Dagens.Items[n] + ' - ' + sch_TodaysGames.items.stri ngs[i];
Main_Dagens.Items[n] := sch_TodaysGames.Items[n] + ' - ' + sch_TodaysGames.items.stri
end;
to this:
Main_Dagens.Items[n] := Main_Dagens.Items[n] + ' - ' + sch_TodaysGames.items.stri
ASKER
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.
Anyway, thanks a bunch for all your help! Everything working just the way I wanted.
ASKER
Great help and patience, thanks a lot.
Open in new window