Recent

Author Topic: Dump a List box to text file?  (Read 4226 times)

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Dump a List box to text file?
« on: January 30, 2018, 11:41:03 pm »


Trying to dump a listbox to a text file.

Isn't Linetext abd lstHangar.Items(IDX) both strings?


Compile Project, Target: FlightPlans.exe: Exit code 1, Errors: 2
uconfig.pas(245,30) Error: Incompatible types: got "TStrings" expected "AnsiString"
uconfig.pas(245,36) Fatal: Syntax error, "EXCEPT" expected but "(" found


Code: Pascal  [Select][+][-]
  1. 235 procedure TConfigDlg.btmAcceptHangarClick(Sender: TObject);
  2. 236 var
  3. 237  IDX        : Integer;
  4. 238  aFile      : TextFile;
  5. 239  Linetext   : string;
  6. 240 begin
  7. 241   AssignFile(aFile, HangarText);
  8. 242   try
  9. 243     Rewrite(aFile);
  10. 244      For IDX := lstHangar.Items.Count - 1 downto 0 do
  11. 245       Linetext := lstHangar.Items(IDX);
  12. 246        WriteLn(aFile, Linetext);
  13. 247   Except
  14. 248        on E: EInOutError do
  15. 249       ShowMessage(C_MSG2);
  16. 250   end;
  17. 251      CloseFile(aFile);
  18. 252end;    
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

Mick

  • Jr. Member
  • **
  • Posts: 51
Re: Dump a List box to text file?
« Reply #1 on: January 31, 2018, 12:14:46 am »
You are trying to access the Items which is an indexed property, not a method with IDX as an argument.
Therefore the usage of .Items(IDX) needs to be replaced with .Items[IDX]. Your code:
Code: Pascal  [Select][+][-]
  1. For IDX := lstHangar.Items.Count - 1 downto 0 do
  2.   Linetext := lstHangar.Items(IDX); // <<<<<< Use [] brackets here instead of ()
  3.     WriteLn(aFile, Linetext);
So, just use square brackets instead of plain, round brackets.

There is also a problem with your loop. Your code:
Code: Pascal  [Select][+][-]
  1. For IDX := lstHangar.Items.Count - 1 downto 0 do
  2.   Linetext := lstHangar.Items(IDX);
  3.     WriteLn(aFile, Linetext);
You will only write the the Linetext variable in the last iteration of the loop.
You should have two lines of code in the loop body, but you only have one.
That's because you forgot to wrap these two lines with begin/end pair.
Code: Pascal  [Select][+][-]
  1. For IDX := lstHangar.Items.Count - 1 downto 0 do
  2. begin
  3.   Linetext := lstHangar.Items(IDX);
  4.   WriteLn(aFile, Linetext);
  5. end;
  6.  

And your iteration with downto will cause that you have the lines of text in the reverse order, because you are iterating from last to first item, but maybe it is what you want? Your code:
Code: Pascal  [Select][+][-]
  1. For IDX := lstHangar.Items.Count - 1 downto 0 do
If you need to have the real dump (not reversed order) you should use normal loop from first to last item:
Code: Pascal  [Select][+][-]
  1. For IDX := 0 to lstHangar.Items.Count - 1 do

BTW, you can try to use built-in functions of the TStrings class to save to file.
Code: Pascal  [Select][+][-]
  1. lstHangar.Items.SaveToFile();


JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: Dump a List box to text file?
« Reply #2 on: January 31, 2018, 12:46:03 am »
lstHangar.Items.SaveToFile()

WOW your kidding right.

The order they are saved is unimportant. I sort when I load them back in. I think I will correct my code so I understand. Once I get that down I'll switch to "lstHangar.Items.SaveToFile()".

Too bad they don't have a load from file.

Thanks for the help.
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Dump a List box to text file?
« Reply #3 on: January 31, 2018, 12:58:33 am »
What makes you think they don't have a LoadfromFile? :P

Look harder its there..

Also, when you sort the list it gets saved as in the results of the sort.

The only true wisdom is knowing you know nothing

GAN

  • Sr. Member
  • ****
  • Posts: 370
Re: Dump a List box to text file?
« Reply #4 on: January 31, 2018, 01:01:48 am »

Too bad they don't have a load from file.


Yes, ListBox1.Items.LoadFromFile (const filename);

Lazarus 2.0.8 FPC 3.0.4 Linux Mint Mate 19.3
Zeos 7̶.̶2̶.̶6̶ 7.1.3a-stable - Sqlite 3.32.3 - LazReport

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: Dump a List box to text file?
« Reply #5 on: January 31, 2018, 01:53:53 am »
What makes you think they don't have a LoadfromFile? :P

Look harder its there..

Also, when you sort the list it gets saved as in the results of the sort.

Well I'm a newbe. I'm not allowed to know those things off hand and beside;

"It's very difficult to know what you don't know."
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

Mick

  • Jr. Member
  • **
  • Posts: 51
Re: Dump a List box to text file?
« Reply #6 on: January 31, 2018, 03:13:45 am »
@JLWest, just a hint for a newbie: if you don't know this yet - you can find many interesting things like the list of methods, properties etc. when you hit Ctrl+Space in the Lazarus code editor.

It will invoke the code completion tool, which can be very helpful in case if you are not sure what are the members (methods, properties, etc) of any type (class, interface, record, etc). available for you.

In your case, when you will hit this key combination after a dot following the Items property, you will find the list of methods and properties of the TStrings class (which is a "container" for the items of the TListbox component).

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: Dump a List box to text file?
« Reply #7 on: January 31, 2018, 04:24:14 am »
Thanks - I'll try using it more. But a lot of times I don't understand what it's trying to tell me. So I post and someone usually helps me out.

It's not that I'm lazy, I have taken one online course $12.00, it had a few things but mostly "Hello World".

Then I did another site LAZPlanet and about 20 to 30 programs, and a lot of reading. But I've only been at this for two week. And of course YouTube.

For a beginner, I usually do a google search for say "File handling Free Pascal".

I am somewhat surprised at the Lazarus site and the Free Pascal site.

It's suppose to be promoting GUI programing using Free Pascal and Lazarus but looking up information every example is a DOS BOX example. I haven't run across 1 single GUI example.

Yes, I know both Lazarus and  Free Pascal ship with examples. A lot of the Lazarus examples throw errors and for a beginner it's impossible.

 Not complaining, stating the facts.   

FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

balazsszekely

  • Guest
Re: Dump a List box to text file?
« Reply #8 on: January 31, 2018, 05:46:56 am »
@JLWest
Quote
It's suppose to be promoting GUI programing using Free Pascal and Lazarus but looking up information every example is a DOS BOX example. I haven't run across 1 single GUI example.
Lazarus Menu-->Tools-->Example projects (type listbox in the filter for example)

Quote
Not complaining, stating the facts.
You're making progress, that's the important thing. Keep up the good work.

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: Dump a List box to text file?
« Reply #9 on: January 31, 2018, 06:30:10 am »
@Getman

That will help
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

 

TinyPortal © 2005-2018