Recent

Author Topic: StringList not Loading...  (Read 797 times)

OC DelGuy

  • Full Member
  • ***
  • Posts: 217
  • 123
StringList not Loading...
« on: September 18, 2025, 06:06:41 am »
Hi guys, been a while since I last posted!  LOL!
But here I am, back at it...

I have this code, where I need to save my data in a text file.  So, I wrote my Save Procedure and every thing looked fine.  So I then wrote the Load Procedure.  It compiles fine...  But when I run it, it doesn't load the data.  I've seen the text file in between it saving the data and then trying to read it.  The information is there, but it just won't read.  It just runs without data like it's the very first run.


Any Ideas on what I'm doing wrong?

Code: Pascal  [Select][+][-]
  1. unit Ledger;
  2. {$mode ObjFPC}{$H+}
  3. interface
  4. uses Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Grids;
  5. type
  6.   { TForm2 }
  7.   TForm2 = class(TForm)
  8.     BtnBuy, BtnSell, BtnFound: TButton;
  9.     BtnClose: TButton;
  10.     EdtDollars,  EdtHalfs,  EdtQuarts,  EdtDimes,  EdtNicks:  TEdit;
  11.     Label1, Label2, Label3, Label4, Label5: TLabel;
  12.     EdtItem, EdtAmount: TEdit;
  13.     CmbType: TComboBox;
  14.     StringGrid1: TStringGrid;
  15.     procedure BtnBuyClick(Sender: TObject);
  16.     procedure BtnCloseClick(Sender: TObject);
  17.     procedure BtnFoundClick(Sender: TObject);
  18.     procedure BtnSellClick(Sender: TObject);
  19.     procedure FormCreate(Sender: TObject);
  20.   private
  21.     function CoinValueInDollars(AType: string): Double;
  22.     function RemoveCoins(ValueInHalfs: Double): Boolean;
  23.     procedure AddTransaction(const AItem: string; AAmount: Integer; AType: string; IsBuy: Boolean);
  24.     procedure UpdateCoinDisplays;
  25.     procedure AddCoins(AType: string; Amount: Integer);
  26.   public
  27.     function GetTotalInDollars: Double;
  28.     procedure LoadData(const FileName: string);
  29.     procedure SaveData(const FileName: string);
  30.   end;
  31. var
  32.   Form2: TForm2;
  33.  
  34. implementation
  35. {$R *.lfm}
  36. { TForm2 }
  37. procedure TForm2.FormCreate(Sender: TObject);
  38. begin
  39.   // Setup String Grid.
  40.   StringGrid1.ColCount := 3;           // Item, Amount and Type.
  41.   StringGrid1.RowCount := 1;           // Start with an empty row.
  42.   StringGrid1.FixedCols := 0;          // Rows don't have Headers.
  43.   StringGrid1.Cells[0,0] := 'Item';    // Column 0 Header.
  44.   StringGrid1.Cells[1,0] := 'Amount';  // Column 1 Header.
  45.   StringGrid1.Cells[2,0] := 'Type';    // Column 2 Header.
  46.   cmbType.Items.AddStrings(['Dollars','Halfs','Quarts','Dimes','Nicks']);
  47.   UpdateCoinDisplays;
  48. end;
  49.  
  50. procedure TForm2.BtnCloseClick(Sender: TObject);
  51. begin
  52.   ModalResult := mrOK; // returns control to Form1.ShowModal.  Form 1 just  has the Edit Box that shows the coins value in dollars.
  53. end;
  54.  
  55. procedure TForm2.BtnBuyClick(Sender: TObject);
  56. begin AddTransaction(edtItem.Text, StrToIntDef(edtAmount.Text,0), cmbType.Text, True); end;
  57.  
  58. procedure TForm2.BtnFoundClick(Sender: TObject);
  59. begin AddTransaction(edtItem.Text, StrToIntDef(edtAmount.Text,0), cmbType.Text, False); end;
  60.  
  61. procedure TForm2.BtnSellClick(Sender: TObject);
  62. begin AddTransaction(edtItem.Text, StrToIntDef(edtAmount.Text,0), cmbType.Text, False); end;
  63.  
  64. function TForm2.CoinValueInDollars(AType: string): Double;
  65. begin
  66.   case UpperCase(AType) of
  67.     'Dollars': Result := 1;
  68.     'Halfs':   Result := 0.5;
  69.     'Quarts':  Result := 0.25;
  70.     'Dimes':   Result := 0.1;
  71.     'Nicks':   Result := 0.05;
  72.   else
  73.     Result := 0;
  74.   end;
  75. end;
  76.  
  77. function TForm2.RemoveCoins(ValueInHalfs: Double): Boolean;
  78. var
  79.   Remaining: Integer;                                            // work in Nickel units to avoid floating errors.
  80.   CoinValues: array[0..4] of Integer = (20, 10, 5, 2, 1);        // Dollars, Halfs, Quarts, Dimes, Nicks in Nickles.
  81.   CoinCounts: array[0..4] of Integer;
  82.   i, j: Integer;
  83. begin
  84.   // Convert to Nickles
  85.   Remaining := Round(ValueInDollars * 20);
  86.  
  87.   // Load coin counts
  88.   CoinCounts[0] := StrToIntDef(edtDollars.Text,0);
  89.   CoinCounts[1] := StrToIntDef(edtHalfs.Text,0);
  90.   CoinCounts[2] := StrToIntDef(edtQuarts.Text,0);
  91.   CoinCounts[3] := StrToIntDef(edtDimes.Text,0);
  92.   CoinCounts[4] := StrToIntDef(edtNicks.Text,0);
  93.  
  94.   // Pay from lowest denomination upwards
  95.   for i := High(CoinValues) downto Low(CoinValues) do
  96.   begin
  97.     while (Remaining >= CoinValues[i]) and (CoinCounts[i] > 0) do
  98.     begin
  99.       Dec(CoinCounts[i]);
  100.       Dec(Remaining, CoinValues[i]);
  101.     end;
  102.   end;
  103.  
  104.   // If still unpaid, try to break higher coins into smaller
  105.   i := Low(CoinValues);
  106.   while (Remaining > 0) and (i <= High(CoinValues)) do
  107.   begin
  108.     if (Remaining >= CoinValues[i]) then
  109.     begin
  110.       if CoinCounts[i] = 0 then  // need this coin, but do we have?
  111.       begin
  112.        
  113.         j := i-1;
  114.         while (j >= Low(CoinValues)) and (CoinCounts[j] = 0) do Dec(j);  // find next higher coin
  115.         if j >= Low(CoinValues) then
  116.         begin
  117.           // break coin j into smaller ones
  118.           Dec(CoinCounts[j]);
  119.           Inc(CoinCounts[j+1], CoinValues[j] div CoinValues[j+1]);
  120.           Continue; // retry with smaller coins
  121.         end
  122.         else
  123.           Break; // No smaller coins, impossible.
  124.       end
  125.       else
  126.       begin
  127.         Dec(CoinCounts[i]);
  128.         Dec(Remaining, CoinValues[i]);
  129.       end;
  130.     end
  131.     else
  132.       Inc(i);
  133.   end;
  134.  
  135.   Result := Remaining = 0;
  136.  
  137.   if Result then
  138.   begin
  139.     edtDollars.Text := IntToStr(CoinCounts[0]);
  140.     edtHalfs.Text   := IntToStr(CoinCounts[1]);
  141.     edtQuarts.Text  := IntToStr(CoinCounts[2]);
  142.     edtDimes.Text   := IntToStr(CoinCounts[3]);
  143.     edtNicks.Text   := IntToStr(CoinCounts[4]);
  144.   end;
  145. end;
  146.  
  147. function TForm2.GetTotalInDollars: Double;
  148. begin
  149.   Result :=
  150.     StrToIntDef(edtDollars.Text,0)*1 +
  151.     StrToIntDef(edtHalfs.Text,0)*0.5 +
  152.     StrToIntDef(edtQuarts.Text,0)*0.25 +
  153.     StrToIntDef(edtDimes.Text,0)*0.1 +
  154.     StrToIntDef(edtNicks.Text,0)*0.05;
  155. end;
  156.  
  157. procedure TForm2.AddTransaction(const AItem: string; AAmount: Integer; AType: string; IsBuy: Boolean);
  158. var row: Integer;
  159.     Value: Double;
  160. begin
  161.   if AAmount <= 0 then Exit;
  162.  
  163.   Value := AAmount * CoinValueInDollars(AType);
  164.  
  165.   if IsBuy then
  166.   begin
  167.     if RemoveCoins(Value) then
  168.     begin
  169.       row := StringGrid1.RowCount;
  170.       StringGrid1.RowCount     := row+1;
  171.       StringGrid1.Cells[0,row] := AItem;
  172.       StringGrid1.Cells[1,row] := IntToStr(AAmount);
  173.       StringGrid1.Cells[2,row] := AType;
  174.     end
  175.     else
  176.       ShowMessage('Not enough coins!');
  177.   end
  178.   else
  179.   begin
  180.     AddCoins(AType,AAmount);
  181.     row := StringGrid1.RowCount;
  182.     StringGrid1.RowCount     := row+1;
  183.     StringGrid1.Cells[0,row] := AItem;
  184.     StringGrid1.Cells[1,row] := IntToStr(AAmount);
  185.     StringGrid1.Cells[2,row] := AType;
  186.   end;
  187.   UpdateCoinDisplays;
  188. end;
  189.  
  190. procedure TForm2.UpdateCoinDisplays;
  191. begin
  192.   edtDollars.Text := IntToStr(StrToIntDef(edtDollars.Text,0));
  193.   edtHalfs.Text   := IntToStr(StrToIntDef(edtHalfs.Text,0));
  194.   edtQuarts.Text  := IntToStr(StrToIntDef(edtQuarts.Text,0));
  195.   edtDimes.Text   := IntToStr(StrToIntDef(edtDimes.Text,0));
  196.   edtNicks.Text   := IntToStr(StrToIntDef(edtNicks.Text,0));
  197. end;
  198.  
  199. procedure TForm2.AddCoins(AType: string; Amount: Integer);
  200. var v: Integer;
  201. begin
  202.   if Amount <= 0 then Exit;
  203.   if AType='Dollars' then begin v:=StrToIntDef(edtDollars.Text,0)+Amount; edtDollars.Text:=IntToStr(v); end;
  204.   if AType='Halfs' then begin   v:=StrToIntDef(edtHalfs.Text,0)+Amount;   edtHalfs.Text:=IntToStr(v); end;
  205.   if AType='Quarts' then begin  v:=StrToIntDef(edtQuarts.Text,0)+Amount;  edtQuarts.Text:=IntToStr(v); end;
  206.   if AType='Dimes' then begin   v:=StrToIntDef(edtDimes.Text,0)+Amount;   edtDimes.Text:=IntToStr(v); end;
  207.   if AType='Nicks' then begin   v:=StrToIntDef(edtNicks.Text,0)+Amount;   edtNicks.Text:=IntToStr(v); end;
  208. end;
  209.  
  210. procedure TForm2.LoadData(const FileName: string);
  211. var
  212.   SL: TStringList;
  213.   parts: TStringArray;
  214.   i, row: Integer;
  215. begin
  216.   if not FileExists(FileName) then Exit;
  217.   SL := TStringList.Create;
  218.   try
  219.     SL.LoadFromFile(FileName);
  220.     if SL.Count > 0 then
  221.     begin
  222.       parts := SL[0].Split([',']);
  223.       if Length(parts) = 5 then
  224.       begin
  225.         edtDollars.Text := parts[0];
  226.         edtHalfs.Text   := parts[1];
  227.         edtQuarts.Text  := parts[2];
  228.         edtDimes.Text   := parts[3];
  229.         edtNicks.Text   := parts[4];
  230.       end;
  231.     end;
  232.  
  233.     StringGrid1.RowCount := 1;     // Clear grid header.
  234.     for i := 1 to SL.Count-1 do
  235.     begin
  236.       parts := SL[i].Split(['|']);
  237.       if Length(parts) = 3 then
  238.       begin
  239.         row := StringGrid1.RowCount;
  240.         StringGrid1.RowCount := row + 1;
  241.         StringGrid1.Cells[0,row] := parts[0]; // Item
  242.         StringGrid1.Cells[1,row] := parts[1]; // Amount
  243.         StringGrid1.Cells[2,row] := parts[2]; // Type
  244.       end;
  245.     end;
  246.   finally
  247.     SL.Free;
  248.   end;
  249. end;
  250.  
  251. procedure TForm2.SaveData(const FileName: string);
  252. var
  253.   SL: TStringList;
  254.   i: Integer;
  255. begin
  256.   SL := TStringList.Create;
  257.   try
  258.     SL.Add(edtDollars.Text+','+edtHalfs.Text+','+edtQuarts.Text+','+edtDimes.Text+','+edtNicks.Text);                              // Coins first line
  259.     for i := 1 to StringGrid1.RowCount-1 do SL.Add(StringGrid1.Cells[0,i]+'|'+StringGrid1.Cells[1,i]+'|'+StringGrid1.Cells[2,i]);  // Transactions
  260.     SL.SaveToFile(FileName);
  261.   finally
  262.     SL.Free;
  263.   end;
  264. end;
  265.  
  266. end.
  267.  
Free Pascal Lazarus Version #: 2.2.4
Date: 24 SEP 2022
FPC Version: 3.2.2
Revision: Lazarus_2_2_4
x86_64-win64-win32/win64

dbannon

  • Hero Member
  • *****
  • Posts: 3558
    • tomboy-ng, a rewrite of the classic Tomboy
Re: StringList not Loading...
« Reply #1 on: September 18, 2025, 06:32:47 am »
Looks to me like you need to do some debugging.

You have a function, LoadData() that gets passed a file name. It could fail to load the data if the filename is wrong, the file is empty or the data not in the format the function can read.

So, use the debugger to see what it is. If you don't or cannot use the debugger, write a log with a few extra lines in that function telling you what it is doing. Hmm, Windows, no console. Maybe drop a TMemo on the same form and add lines like -

Code: Pascal  [Select][+][-]
  1. TMemo1.Append('LoadData, exiting because file not found : ' + FileName);
  2. ...
  3. TMemo1.Append('LoadData, file loaded with number of lines = ' + inttostr(SL.Count));

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

egsuh

  • Hero Member
  • *****
  • Posts: 1694
Re: StringList not Loading...
« Reply #2 on: September 18, 2025, 07:07:39 am »
As it is text file, you would be able to read the file using Notepad, etc.
And you can add many checking points --- e.g.

Code: Pascal  [Select][+][-]
  1.     SL.LoadFromFile(FileName);
  2.     if SL.Count > 0 then
  3.     begin
  4.       Showmessage(sl[0]);            // look with eyes it is as you expected.
  5.  
  6.       parts := SL[0].Split([',']);
  7.       if Length(parts) = 5 then
  8.       begin
  9.         edtDollars.Text := parts[0];
  10.         edtHalfs.Text   := parts[1];
  11.         edtQuarts.Text  := parts[2];
  12.         edtDimes.Text   := parts[3];
  13.         edtNicks.Text   := parts[4];
  14.       end
  15.  
  16.       else showmessagefmt ('Line has split to %d items', [length(parts)]);   // of course it should  be 5, but who knows...
  17.  
  18.     end;
  19.  
  20.     StringGrid1.RowCount := 1;     // Clear grid header.
  21.     for i := 1 to SL.Count-1 do
  22.     begin
  23.       parts := SL[i].Split(['|']);
  24.       if Length(parts) = 3 then
  25.       begin
  26.         row := StringGrid1.RowCount;
  27.         StringGrid1.RowCount := row + 1;
  28.         StringGrid1.Cells[0,row] := parts[0]; // Item
  29.         StringGrid1.Cells[1,row] := parts[1]; // Amount
  30.         StringGrid1.Cells[2,row] := parts[2]; // Type
  31.       end;
  32.     end;



cdbc

  • Hero Member
  • *****
  • Posts: 2466
    • http://www.cdbc.dk
Re: StringList not Loading...
« Reply #3 on: September 18, 2025, 09:28:52 am »
Hi
Well, your load & save methods look all "Fine and Dandy", such a shame it is, that you never call them...!
Here's a small example of 2 methods where that might take place:
Code: Pascal  [Select][+][-]
  1. procedure TForm2.FormCreate(Sender: TObject);
  2. begin
  3.   // Setup String Grid.
  4.   StringGrid1.ColCount := 3;           // Item, Amount and Type.
  5.   StringGrid1.RowCount := 1;           // Start with an empty row.
  6.   StringGrid1.FixedCols := 0;          // Rows don't have Headers.
  7.   StringGrid1.Cells[0,0] := 'Item';    // Column 0 Header.
  8.   StringGrid1.Cells[1,0] := 'Amount';  // Column 1 Header.
  9.   StringGrid1.Cells[2,0] := 'Type';    // Column 2 Header.
  10.   cmbType.Items.AddStrings(['Dollars','Halfs','Quarts','Dimes','Nicks']);
  11.   UpdateCoinDisplays;
  12.   LoadData('path\to\your\data\somedata.txt'); // <-- maybe HERE?!?
  13. end;
  14.  
  15. procedure TForm2.BtnCloseClick(Sender: TObject);
  16. begin
  17.   SaveData('path\to\your\data\somedata.txt'); // <-- maybe HERE?!?
  18.   ModalResult := mrOK; // returns control to Form1.ShowModal.  Form 1 just  has the Edit Box that shows the coins value in dollars.
  19. end;
I think your problem stems from not calling the methods, but how can you then see the data on disk?!? What are you not showing us?
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6 -> FPC 3.2.2 -> Lazarus 4.0 up until Jan 2025 from then on it's both above &: KDE6/QT6 -> FPC 3.3.1 -> Lazarus 4.99

OC DelGuy

  • Full Member
  • ***
  • Posts: 217
  • 123
Re: StringList not Loading...
« Reply #4 on: September 18, 2025, 10:05:08 am »
Hi
Well, your load & save methods look all "Fine and Dandy", such a shame it is, that you never call them...!
Here's a small example of 2 methods where that might take place:
Code: Pascal  [Select][+][-]
  1. procedure TForm2.FormCreate(Sender: TObject);
  2. begin
  3.   // Setup String Grid.
  4.   StringGrid1.ColCount := 3;           // Item, Amount and Type.
  5.   StringGrid1.RowCount := 1;           // Start with an empty row.
  6.   StringGrid1.FixedCols := 0;          // Rows don't have Headers.
  7.   StringGrid1.Cells[0,0] := 'Item';    // Column 0 Header.
  8.   StringGrid1.Cells[1,0] := 'Amount';  // Column 1 Header.
  9.   StringGrid1.Cells[2,0] := 'Type';    // Column 2 Header.
  10.   cmbType.Items.AddStrings(['Dollars','Halfs','Quarts','Dimes','Nicks']);
  11.   UpdateCoinDisplays;
  12.   LoadData('path\to\your\data\somedata.txt'); // <-- maybe HERE?!?
  13. end;
  14.  
  15. procedure TForm2.BtnCloseClick(Sender: TObject);
  16. begin
  17.   SaveData('path\to\your\data\somedata.txt'); // <-- maybe HERE?!?
  18.   ModalResult := mrOK; // returns control to Form1.ShowModal.  Form 1 just  has the Edit Box that shows the coins value in dollars.
  19. end;
I think your problem stems from not calling the methods, but how can you then see the data on disk?!? What are you not showing us?
Regards Benny


No, I am calling them...  The Save works nice.   :) :D  It's the Load that's giving me a problem! >:(



Code: Pascal  [Select][+][-]
  1. unit Wealth;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Ledger;
  5. type
  6.   { TForm1 }
  7.   TForm1 = class(TForm)
  8.     EdtTotalDolls: TEdit;
  9.     procedure EdtTotalDollsClick(Sender: TObject);
  10.     procedure FormActivate(Sender: TObject);
  11.     procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
  12.     procedure FormCreate(Sender: TObject);
  13.   private
  14.     procedure UpdateTotal;
  15.   public
  16.   end;
  17. var
  18.   Form1: TForm1;
  19.  
  20. implementation
  21. {$R *.lfm}
  22. { TForm1 }
  23. procedure TForm1.EdtTotalDollsClick(Sender: TObject);
  24. begin
  25.   if not Assigned(Form2) then Application.CreateForm(TForm2, Form2);
  26.   Form2.ShowModal;
  27.   UpdateTotal;
  28. end;
  29.  
  30. procedure TForm1.FormActivate(Sender: TObject);
  31. begin UpdateTotal; end;
  32.  
  33. procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
  34. begin if Assigned(Form2) then Form2.SaveData('Wealth.txt'); end;
  35.  
  36. procedure TForm1.FormCreate(Sender: TObject);
  37. begin
  38.   if not Assigned(Form2) then Application.CreateForm(TForm2, Form2);
  39.   Form2.LoadData('Wealth.txt');
  40. end;
  41.  
  42. procedure TForm1.UpdateTotal;
  43. begin
  44.   if Assigned(Form2) then edtTotalDolls.Text := FloatToStrF(Form2.GetTotalInDollars, ffFixed, 10, 2) else edtTotalDolls.Text := '0.00';
  45. end;
  46.  
  47. end.
  48.  
Free Pascal Lazarus Version #: 2.2.4
Date: 24 SEP 2022
FPC Version: 3.2.2
Revision: Lazarus_2_2_4
x86_64-win64-win32/win64

OC DelGuy

  • Full Member
  • ***
  • Posts: 217
  • 123
Re: StringList not Loading...
« Reply #5 on: September 18, 2025, 10:14:55 am »
As it is text file, you would be able to read the file using Notepad, etc.
And you can add many checking points --- e.g.

Code: Pascal  [Select][+][-]
  1.     SL.LoadFromFile(FileName);
  2.     if SL.Count > 0 then
  3.     begin
  4.       Showmessage(sl[0]);            // look with eyes it is as you expected.
  5.  
  6.       parts := SL[0].Split([',']);
  7.       if Length(parts) = 5 then
  8.       begin
  9.         edtDollars.Text := parts[0];
  10.         edtHalfs.Text   := parts[1];
  11.         edtQuarts.Text  := parts[2];
  12.         edtDimes.Text   := parts[3];
  13.         edtNicks.Text   := parts[4];
  14.       end
  15.  
  16.       else showmessagefmt ('Line has split to %d items', [length(parts)]);   // of course it should  be 5, but who knows...
  17.  
  18.     end;
  19.  
  20.     StringGrid1.RowCount := 1;     // Clear grid header.
  21.     for i := 1 to SL.Count-1 do
  22.     begin
  23.       parts := SL[i].Split(['|']);
  24.       if Length(parts) = 3 then
  25.       begin
  26.         row := StringGrid1.RowCount;
  27.         StringGrid1.RowCount := row + 1;
  28.         StringGrid1.Cells[0,row] := parts[0]; // Item
  29.         StringGrid1.Cells[1,row] := parts[1]; // Amount
  30.         StringGrid1.Cells[2,row] := parts[2]; // Type
  31.       end;
  32.     end;

Yep!   Used Notepad.  Text file looks Beautiful!  :-*  O:-)

I did have a TMemo, but it doesn't show up as it's loading, so it was useless.  I didn't keep the code, because I re-wrote the whole thing.  I was frustrated a bit, so I dumped the whole folder in the recycle bin and started over.  Same thing.
Free Pascal Lazarus Version #: 2.2.4
Date: 24 SEP 2022
FPC Version: 3.2.2
Revision: Lazarus_2_2_4
x86_64-win64-win32/win64

cdbc

  • Hero Member
  • *****
  • Posts: 2466
    • http://www.cdbc.dk
Re: StringList not Loading...
« Reply #6 on: September 18, 2025, 10:19:53 am »
Hi
Hmmm, then you might want to upload the /saved/ datafile, so that we can try loading it ourselves, in order to help you debug this B*tch...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6 -> FPC 3.2.2 -> Lazarus 4.0 up until Jan 2025 from then on it's both above &: KDE6/QT6 -> FPC 3.3.1 -> Lazarus 4.99

OC DelGuy

  • Full Member
  • ***
  • Posts: 217
  • 123
Re: StringList not Loading...
« Reply #7 on: September 18, 2025, 10:23:27 am »
Looks to me like you need to do some debugging.

You have a function, LoadData() that gets passed a file name. It could fail to load the data if the filename is wrong, the file is empty or the data not in the format the function can read.

So, use the debugger to see what it is. If you don't or cannot use the debugger, write a log with a few extra lines in that function telling you what it is doing. Hmm, Windows, no console. Maybe drop a TMemo on the same form and add lines like -

Code: Pascal  [Select][+][-]
  1. TMemo1.Append('LoadData, exiting because file not found : ' + FileName);
  2. ...
  3. TMemo1.Append('LoadData, file loaded with number of lines = ' + inttostr(SL.Count));

Davo

This is going to sound really stupid, but I don't know how to use the debugger!  :-[  Seriously, I don't even know how to start it!  :o  :(

I did the TMemo thing too, but then trashed it and started over.  I was kind of ticked off because I couldn't see a problem in the code.   So I was only able to load the second draft of my code.
Free Pascal Lazarus Version #: 2.2.4
Date: 24 SEP 2022
FPC Version: 3.2.2
Revision: Lazarus_2_2_4
x86_64-win64-win32/win64

Bart

  • Hero Member
  • *****
  • Posts: 5614
    • Bart en Mariska's Webstek
Re: StringList not Loading...
« Reply #8 on: September 18, 2025, 10:34:34 am »
Poor man's debugging:

First step: after SL.LoadFromFile(FileName), verify that it loads the expected text:
Code: Pascal  [Select][+][-]
  1.   showmessage('SL.Text='+LineEnding+SL.Text);

Second:
If that's OK, put debugging statements like that for each line of code in the "decoding" part to see if you get what you want.

W.r.t. to the debugger: in Lazarus you can set a breakpoint on the first line of the procedure that does the loading/parsing (move the cursor to that line and press F5).
Then from within the IDE start the program.
Once your program reaches that line, the program halts.
You can then step over the code using F8 and inspect variables (hoover the mouse over them, mind you: not always totally reliable).

Bart
« Last Edit: September 18, 2025, 04:55:11 pm by Bart »

jamie

  • Hero Member
  • *****
  • Posts: 7308
Re: StringList not Loading...
« Reply #9 on: September 18, 2025, 12:06:39 pm »
I have a question more than a solution how many times will the application create form execute without actually testing if it already exists?
In the project main file is that form also inserted as Auto create?

The reason I ask is it keeps checking to see if form two is valid and just calls application create form which may have already been done or will be done again since form one will be created first most likely and during the creation of form one is checking form two which hasn't been executed yet in the main file.

I wonder how much memory leak there is?
The only true wisdom is knowing you know nothing

OC DelGuy

  • Full Member
  • ***
  • Posts: 217
  • 123
Re: StringList not Loading...
« Reply #10 on: September 18, 2025, 07:20:04 pm »
Hey guys, I just had a thought...  :o

When I run the application a second time, I call the Load procedure from Form 1.
Correct me if I'm wrong, but I think, I haven't CREATED Form 2 yet.
So I'm loading up StringGrid1 with data from SL.
Correct me if I'm wrong, but then AFTER the loading, I CREATE Form 2, which triggers TForm2.FormCreate.

Is the TForm2.FormCreate trampling all over the loaded data?  Specially the StringGrid1.RowCount := 1; part?  Did I just erase all the rows created in the Load Procedure?
Free Pascal Lazarus Version #: 2.2.4
Date: 24 SEP 2022
FPC Version: 3.2.2
Revision: Lazarus_2_2_4
x86_64-win64-win32/win64

cdbc

  • Hero Member
  • *****
  • Posts: 2466
    • http://www.cdbc.dk
Re: StringList not Loading...
« Reply #11 on: September 18, 2025, 07:44:45 pm »
Hi
Quote
Is the TForm2.FormCreate trampling all over the loaded data?  Specially the StringGrid1.RowCount := 1; part?  Did I just erase all the rows created in the Load Procedure?
Probably. I did think there was something you weren't showing us...
/b
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6 -> FPC 3.2.2 -> Lazarus 4.0 up until Jan 2025 from then on it's both above &: KDE6/QT6 -> FPC 3.3.1 -> Lazarus 4.99

 

TinyPortal © 2005-2018