Recent

Author Topic: Persistent properties  (Read 3050 times)

JonBondy

  • New Member
  • *
  • Posts: 27
Persistent properties
« on: March 30, 2024, 03:19:51 pm »
Is there an easy way to store properties between program invocations?  Like when the user re-positions a form, or widens it, and you want the form to be where s/he last left it?

I used to use iniFiles, but I imagine some progress has been made.

Thanks!

Jon

Handoko

  • Hero Member
  • *****
  • Posts: 5426
  • My goal: build my own game engine using Lazarus
Re: Persistent properties
« Reply #1 on: March 30, 2024, 03:56:24 pm »
Done.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, IniFiles;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
  16.     procedure FormCreate(Sender: TObject);
  17.   end;
  18.  
  19. const
  20.   IniFile         = 'Config.ini';
  21.   PositionSection = 'Position';
  22.   SizeSection     = 'Size';
  23.   LeftID          = 'Left';
  24.   TopID           = 'Top';
  25.   WidthID         = 'Width';
  26.   HeightID        = 'Height';
  27.  
  28. var
  29.   Form1: TForm1;
  30.  
  31. implementation
  32.  
  33. {$R *.lfm}
  34.  
  35. { TForm1 }
  36.  
  37. procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
  38. var
  39.   IniData: TIniFile;
  40. begin
  41.   IniData := TIniFile.Create(IniFile);
  42.   IniData.WriteInteger(PositionSection, LeftID, Left);
  43.   IniData.WriteInteger(PositionSection, TopID, Top);
  44.   IniData.WriteInteger(SizeSection, WidthID, Width);
  45.   IniData.WriteInteger(SizeSection, HeightID, Height);
  46.   IniData.Free;
  47. end;
  48.  
  49. procedure TForm1.FormCreate(Sender: TObject);
  50. var
  51.   IniData: TIniFile;
  52. begin
  53.  
  54.   Constraints.MinHeight := 240;
  55.   Constraints.MinWidth  := 320;
  56.   Constraints.MaxHeight := 1080;
  57.   Constraints.MaxWidth  := 1920;
  58.  
  59.   if FileExists(IniFile) then
  60.   begin
  61.     IniData := TIniFile.Create(IniFile);
  62.     Left    := IniData.ReadInteger(PositionSection, LeftID, 0);
  63.     Top     := IniData.ReadInteger(PositionSection, TopID, 0);
  64.     Width   := IniData.ReadInteger(SizeSection, WidthID, 320);
  65.     Height  := IniData.ReadInteger(SizeSection, HeightID, 240);
  66.     IniData.Free;
  67.   end;
  68.  
  69. end;
  70.  
  71. end.

dsiders

  • Hero Member
  • *****
  • Posts: 1408
Re: Persistent properties
« Reply #2 on: March 30, 2024, 04:00:53 pm »
There are also classes like TIniPropStorage, TJSONPropStorage, and TXMLPropStorage.
Demos in: examples/propstorage.
Preview the next Lazarus documentation release at: https://dsiders.gitlab.io/lazdocsnext

Handoko

  • Hero Member
  • *****
  • Posts: 5426
  • My goal: build my own game engine using Lazarus
Re: Persistent properties
« Reply #3 on: March 30, 2024, 04:04:12 pm »
Also, one can use TRegistry to store the data.

https://wiki.freepascal.org/fcl-registry

wp

  • Hero Member
  • *****
  • Posts: 12800
Re: Persistent properties
« Reply #4 on: March 30, 2024, 04:55:00 pm »
There are also classes like TIniPropStorage, TJSONPropStorage, and TXMLPropStorage.
Demos in: examples/propstorage.
And there is a tutorial-like wiki-article: https://wiki.freepascal.org/TXMLPropStorage; it is for XMLPropStorage, but can be applied to IniPropStorage and JSONPropstorage in the same way.

JonBondy

  • New Member
  • *
  • Posts: 27
Re: Persistent properties
« Reply #5 on: March 30, 2024, 05:13:36 pm »
Thank you!

Sorry for my cluelessness, but where are the demos/examples found in the menu tree?

And where is TIniPropStorage.  More generally, how do I find a particular class via a search?

Jon

JonBondy

  • New Member
  • *
  • Posts: 27
Re: Persistent properties
« Reply #6 on: March 30, 2024, 05:28:53 pm »
I wandered around and eventually stumbled across TIniPropStorage.  Documentation says there will be a SessionProperties property, but all I can see is StoredValues.  Have created a StoredValue, but all I can see there are KeyString, Name, and Value.  I was hoping to see a list of components and component properties.  What I am seeing is nothing like the documentation I have found online (in multiple locations).

Thanks

JonBondy

  • New Member
  • *
  • Posts: 27
Re: Persistent properties
« Reply #7 on: March 30, 2024, 05:31:13 pm »
Never mind.  I found the Session Properties.  There was a [transparent] Panel on the form, and I thought I had clicked on the Form, but in fact had clicked on the Panel.

dsiders

  • Hero Member
  • *****
  • Posts: 1408
Re: Persistent properties
« Reply #8 on: March 30, 2024, 06:09:37 pm »
Sorry for my cluelessness, but where are the demos/examples found in the menu tree?

If you can't just open the file in the directory provided, then Tools > Example Projects.

And where is TIniPropStorage.  More generally, how do I find a particular class via a search?

View > Code Browser > Show Identifiers   and enter the class name you're looking for. Or right click on the component in the component palette and use Open Unit.
Preview the next Lazarus documentation release at: https://dsiders.gitlab.io/lazdocsnext

 

TinyPortal © 2005-2018