Recent

Author Topic: Pass Value to FormCreate while Creating Form [SOLVED]  (Read 1334 times)

AlphaInc.

  • Jr. Member
  • **
  • Posts: 93
Pass Value to FormCreate while Creating Form [SOLVED]
« on: April 23, 2021, 05:19:31 pm »
Hello everybody,

I try to load the Size of my Screen off a .ini-file which I use for other declarations as well (for example Button Captions).
Therefore I have a procedure, which checks the .ini-file file and sets all captions. The value for used for the size of the tool (defined as Factor in the private section of the tool) which is set in the ComboSettings procedure should be used in the FromCreate-Procedure. The code does compile but I get an "External SIGFPE" error pointing directly at line 82:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     ComboBox1: TComboBox;
  17.  
  18.     procedure Button1Click(Sender: TObject);
  19.     procedure ComboSetting();
  20.     procedure FormCreate(Sender: TObject);
  21.  
  22.   private
  23.     sModsLine:         String;
  24.     sScreenSize:       String;
  25.     Faktor:            extended;
  26.  
  27.   public
  28.  
  29.   end;
  30.  
  31. var
  32.   Form1: TForm1;
  33.  
  34. const cMyFileName='..\Binaries\conf.ini';
  35.  
  36. implementation
  37.  
  38. {$R *.lfm}
  39.  
  40. procedure TForm1.Button1Click(Sender: TObject);
  41. begin
  42.  
  43. end;
  44.  
  45. procedure TForm1.ComboSetting();
  46. var
  47.    MyFile:Text;
  48.    Line5,Line6:String;
  49. begin
  50.    AssignFile(MyFile,cMyFileName);
  51.    Reset(MyFile);
  52.    ReadLn(MyFile,Line5);
  53.    ReadLn(MyFile,Line6);
  54.  
  55.    sModsLine:=Line5;
  56.    sScreenSize:=Line6;
  57.  
  58.    CloseFile(MyFile);
  59.  
  60.    if sScreenSize='size=XL' then
  61.       begin
  62.          Faktor:=1;
  63.       end
  64.    else if sScreenSize='size=L' then
  65.       begin
  66.          Faktor:=1.6;
  67.       end
  68.    else if sScreenSize='size=M' then
  69.       begin
  70.          Faktor:=2.4;
  71.       end
  72.    else if sScreenSize='size=S' then
  73.       begin
  74.          Faktor:=3.6;
  75.       end;
  76.  
  77. end;
  78.  
  79. procedure TForm1.FormCreate(Sender: TObject);
  80. begin
  81.    ComboSetting;
  82.    Width:=round(Screen.Width/Faktor);
  83.    Height:=round(Screen.Height/Faktor);
  84. end;
  85.  
  86. end.
  87.  

Edit: I created a new project just for testing this since I don't want to "destroy" my running tool. That's why the reading of the .ini is in another procedure instead of the FormCreate and also why other buttons are missing. But nevertheless, I need to get it running.

Anyway, how do you set the Factor size via the reading on an .ini ?
« Last Edit: April 23, 2021, 08:26:23 pm by AlphaInc. »

h-elsner

  • Newbie
  • Posts: 6
Re: Pass Value to FormCreate while Creating Form
« Reply #1 on: April 23, 2021, 05:50:08 pm »
I use SessionProperties of the TForm in combination with TXMLPropStorage. There is also a TIniPropStorage as well as a TJSONPropStorage. It provides all needed fuctionality to store settings of your application in one of the 3 formates that you like.
Also own values can be defined and stored there. I avoid such complicated ways if a tested and nice to use component is available.

If you want to change things afterwards do it in the OnShow event.

br HE

dseligo

  • Hero Member
  • *****
  • Posts: 1194
Re: Pass Value to FormCreate while Creating Form
« Reply #2 on: April 23, 2021, 07:28:39 pm »
The code does compile but I get an "External SIGFPE" error pointing directly at line 82:

You don't have default value for variable 'Faktor'. It is probably 0, so you get the error.
Try to set some default and then troubleshoot why it isn't set (probably your error in reading file).

dseligo

  • Hero Member
  • *****
  • Posts: 1194
Re: Pass Value to FormCreate while Creating Form
« Reply #3 on: April 23, 2021, 07:33:01 pm »
Try this in your code:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.ComboSetting();
  2. var
  3.    MyFile:Text;
  4.    Line5,Line6:String;
  5. begin
  6.    AssignFile(MyFile,cMyFileName);
  7.    Reset(MyFile);
  8.  
  9.   // !!! Maybe you need to read first four lines here, before lines 5 and 6?
  10.  
  11.    ReadLn(MyFile,Line5);
  12.    ReadLn(MyFile,Line6);
  13.  
  14.    sModsLine:=Line5;
  15.    sScreenSize:=Line6;
  16.   ShowMessage('sScreenSize is: "'+sScreenSize+'"');
  17.  
  18.    CloseFile(MyFile);
  19.  
  20.    if sScreenSize='size=XL' then
  21.       begin
  22.          Faktor:=1;
  23.       end
  24.    else if sScreenSize='size=L' then
  25.       begin
  26.          Faktor:=1.6;
  27.       end
  28.    else if sScreenSize='size=M' then
  29.       begin
  30.          Faktor:=2.4;
  31.       end
  32.    else if sScreenSize='size=S' then
  33.       begin
  34.          Faktor:=3.6;
  35.       end;
  36.  
  37. end;
  38.  
  39. procedure TForm1.FormCreate(Sender: TObject);
  40. begin
  41.    ComboSetting;
  42.   ShowMessage('Faktor is:'+FloatToStr(FaktorI));
  43.  
  44.   If Faktor<>0 then
  45.   begin
  46.     Width:=round(Screen.Width/Faktor);
  47.     Height:=round(Screen.Height/Faktor);
  48.   end;
  49. end;
  50.  
  51. end.
  52.  

AlphaInc.

  • Jr. Member
  • **
  • Posts: 93
Re: Pass Value to FormCreate while Creating Form
« Reply #4 on: April 23, 2021, 08:26:13 pm »
Try this in your code:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.ComboSetting();
  2. var
  3.    MyFile:Text;
  4.    Line5,Line6:String;
  5. begin
  6.    AssignFile(MyFile,cMyFileName);
  7.    Reset(MyFile);
  8.  
  9.   // !!! Maybe you need to read first four lines here, before lines 5 and 6?
  10.  
  11.    ReadLn(MyFile,Line5);
  12.    ReadLn(MyFile,Line6);
  13.  
  14.    sModsLine:=Line5;
  15.    sScreenSize:=Line6;
  16.   ShowMessage('sScreenSize is: "'+sScreenSize+'"');
  17.  
  18.    CloseFile(MyFile);
  19.  
  20.    if sScreenSize='size=XL' then
  21.       begin
  22.          Faktor:=1;
  23.       end
  24.    else if sScreenSize='size=L' then
  25.       begin
  26.          Faktor:=1.6;
  27.       end
  28.    else if sScreenSize='size=M' then
  29.       begin
  30.          Faktor:=2.4;
  31.       end
  32.    else if sScreenSize='size=S' then
  33.       begin
  34.          Faktor:=3.6;
  35.       end;
  36.  
  37. end;
  38.  
  39. procedure TForm1.FormCreate(Sender: TObject);
  40. begin
  41.    ComboSetting;
  42.   ShowMessage('Faktor is:'+FloatToStr(FaktorI));
  43.  
  44.   If Faktor<>0 then
  45.   begin
  46.     Width:=round(Screen.Width/Faktor);
  47.     Height:=round(Screen.Height/Faktor);
  48.   end;
  49. end;
  50.  
  51. end.
  52.  

Ah okay, it was because it did not read the other lines. Thank you for helping me finding the solution :)

dseligo

  • Hero Member
  • *****
  • Posts: 1194
Re: Pass Value to FormCreate while Creating Form
« Reply #5 on: April 23, 2021, 10:56:42 pm »
Ah okay, it was because it did not read the other lines. Thank you for helping me finding the solution :)

You're welcome.
But still, you should have either a default value for Faktor or check that Faktor<>0 in case that someone messes with your ini file.

AlphaInc.

  • Jr. Member
  • **
  • Posts: 93
Re: Pass Value to FormCreate while Creating Form
« Reply #6 on: April 23, 2021, 11:08:49 pm »
Ah okay, it was because it did not read the other lines. Thank you for helping me finding the solution :)

You're welcome.
But still, you should have either a default value for Faktor or check that Faktor<>0 in case that someone messes with your ini file.
That SHOULDN'T be the case but I'll implement a line for that case.
Would that be solved with a single else statement (after checking Faktor <> 0) where you set Faktor manually?

Code: Pascal  [Select][+][-]
  1. begin
  2. If Faktor<>0 then
  3.   begin
  4.     Width:=round(Screen.Width/Faktor);
  5.     Height:=round(Screen.Height/Faktor);
  6.   end
  7. else
  8.   begin
  9.     Width:=round(Screen.Width/2); // for example 2
  10.     Height:=round(Screen.Height/2);
  11.   end;    
  12. end;

dbannon

  • Hero Member
  • *****
  • Posts: 2786
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Pass Value to FormCreate while Creating Form [SOLVED]
« Reply #7 on: April 24, 2021, 02:17:49 am »
You seem to be using the term ".ini-file" a little differently than I would.

I suggest if you use an ini file, you should only read and write to it with the ini file functions designed to do just that.  For example, when reading an ini file, you use -

Code: Pascal  [Select][+][-]
  1. Configfile.ReadString('BasicSettings', 'size', 'S')

And the ini file subsystem will search the whole file, not just one particular line, for a 'size' entry under 'BasicSettings'.  If it does not find a suitable entry, it applies the default, 'S' and you app can continue quite safely.  Your model, at the moment appears to depend on the entries appearing in a particular line of the file and if something else is there, may return any data it finds. Users expect to be able to manually edit an ini file if they need to and most certainly won't respect line numbers.

And perhaps a case statement instead of the confusing if, else ... array would look prettier.

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

 

TinyPortal © 2005-2018