Recent

Author Topic: Save form contents to file.  (Read 3323 times)

gjunk

  • New Member
  • *
  • Posts: 33
Save form contents to file.
« on: April 04, 2024, 12:24:21 pm »
Is there a simple way to save the contents of the fields in a form to a file?
I don't need properties or buttons or labels, I just need the values entered into the edit,date, time and checkbox fields.
I know I can use Memo1.Lines.LoadFromFile(Fname) or SaveTofile to read or write a memo file.

Is there something that can save the contents of a form fields to a file and read it back?
 

cdbc

  • Hero Member
  • *****
  • Posts: 1162
    • http://www.cdbc.dk
Re: Save form contents to file.
« Reply #1 on: April 04, 2024, 01:20:25 pm »
Hi
It's possible with e.g.: a combination of RTTI & an Ini-file... and a bit of elbow-grease  :D
I have done an ancester-settings-class that employs this technique.
If you feel like it I could share the code with you?
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

gjunk

  • New Member
  • *
  • Posts: 33
Re: Save form contents to file.
« Reply #2 on: April 04, 2024, 01:25:03 pm »
Thank you.
I would appreciate a look at it.

alpine

  • Hero Member
  • *****
  • Posts: 1111
Re: Save form contents to file.
« Reply #3 on: April 04, 2024, 01:40:44 pm »
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

cdbc

  • Hero Member
  • *****
  • Posts: 1162
    • http://www.cdbc.dk
Re: Save form contents to file.
« Reply #4 on: April 04, 2024, 02:05:16 pm »
Hi
Right, so here is a descendant class to show how the base class is used:
Code: Pascal  [Select][+][-]
  1. {***************************************************************
  2. * 17.01.2023 /bc once was a simple app settings persistence    *
  3. ***************************************************************}
  4. unit opt_ledger;
  5. {$mode objfpc}{$H+}
  6. interface
  7. uses
  8.   Classes, SysUtils, bc_appoptions;
  9. type
  10.   { TKKLOptions is a simple settings object }
  11.   TKKLOptions = class(TbcOptions)  // TPersistent
  12.   private
  13.     fArchiveDir: string;
  14.     fAutoSave: boolean;
  15.     fBackupDir: string;
  16.     fCaseSensitive: boolean;
  17.     fHighlightSumRow: boolean;
  18.     fLineLen: ptruint; { max should be 94 utf8-chars }
  19.     fMatchAll: boolean;
  20.     fRemoveItemOnClose: boolean;
  21.     fScrollToLast: boolean;
  22.     fSearchMultiple: boolean;
  23.     fWholeWords: boolean;
  24.     fWorkingDir: string;
  25.   protected
  26.     procedure Init; override;
  27.   public
  28.     constructor Create;
  29.     destructor Destroy; override;
  30.   published
  31.     property ArchiveDir: string read fArchiveDir write fArchiveDir;
  32.     property AutoSave: boolean read fAutoSave write fAutoSave;
  33.     property BackupDir: string read fBackupDir write fBackupDir;
  34.     property CaseSensitive: boolean read fCaseSensitive write fCaseSensitive;
  35.     property HighlightSumRow: boolean read fHighlightSumRow write fHighlightSumRow;
  36.     property LineLen: ptruint read fLineLen write fLineLen;
  37.     property MatchAll: boolean read fMatchAll write fMatchAll;
  38.     property RemoveItemOnClose: boolean read fRemoveItemOnClose write fRemoveItemOnClose;
  39.     property ScrollToLast: boolean read fScrollToLast write fScrollToLast;
  40.     property SearchMultiple: boolean read fSearchMultiple write fSearchMultiple;
  41.     property WholeWords: boolean read fWholeWords write fWholeWords;
  42.     property WorkingDir: string read fWorkingDir write fWorkingDir;
  43.   end;
  44.  
  45. function KKLOptions: TKKLOptions; { factory for singleton }
  46.  
  47. implementation
  48. uses bc_advstring;
  49. var
  50.   Singleton: TKKLOptions;
  51.  
  52. function KKLOptions: TKKLOptions;
  53. begin
  54.   if not assigned(Singleton) then Singleton:= TKKLOptions.Create;
  55.   Result:= Singleton;
  56. end;
  57.  
  58. { TKKLOptions }
  59. procedure TKKLOptions.Init;
  60. begin
  61.   inherited Init; { sets the name prop }
  62.   fArchiveDir:= bcGetAppDir; { could also use GetCurrentDir from sysutils }
  63.   fAutoSave:= false;
  64.   fBackupDir:= bcGetAppDir;
  65.   fCaseSensitive:= false;
  66.   fHighlightSumRow:= true;
  67.   fLineLen:= 94;
  68.   fMatchAll:= true;
  69.   fRemoveItemOnClose:= true;
  70.   fScrollToLast:= true;
  71.   fSearchMultiple:= false;
  72.   fWholeWords:= false;
  73.   fWorkingDir:= bcGetAppDir;
  74. end;
  75.  
  76. constructor TKKLOptions.Create;
  77. begin
  78.   inherited Create; { calls init before it reads the ini-file }
  79. end;
  80.  
  81. destructor TKKLOptions.Destroy;
  82. begin
  83.   inherited Destroy;
  84. end;
  85.  
  86. initialization
  87.   Singleton:= nil;
  88. finalization
  89.   if assigned(Singleton) then FreeAndNil(Singleton);
  90. end.
  91.  
And in the attachment, you'll find the ancester-class...
edit: Sorry I forgot 1 file... and the base class descends from TPersistent, which means, it implements the IFPObserved interface -> just write an observer for it and you'll get *Live* updates of changes  8)
Have fun  8-)
Regards Benny
« Last Edit: April 04, 2024, 03:11:20 pm by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

gjunk

  • New Member
  • *
  • Posts: 33
Re: Save form contents to file.
« Reply #5 on: April 04, 2024, 03:10:41 pm »
Thank you.

Edson

  • Hero Member
  • *****
  • Posts: 1302
Re: Save form contents to file.
« Reply #6 on: April 04, 2024, 08:22:53 pm »
It's like using a configuration form. I have a library for that https://github.com/t-edson/MiConfig

It needs first to associate controls to variables in order to save/load to disk, but maybe can help.
Lazarus 2.2.6 - FPC 3.2.2 - x86_64-win64 on Windows 10

gjunk

  • New Member
  • *
  • Posts: 33
Re: Save form contents to file.
« Reply #7 on: April 10, 2024, 04:36:29 pm »
I am using  xmlpropstorage and as I was adding edit and timeedit fields to the sessionproperties, the text selection for the
property is no longer appearing as a selection.
Is there a limit on how many fields I can add?
I have added 17 so far.
Do I need to use multiple xmlpropstorage?
How do I get it back?

gjunk

  • New Member
  • *
  • Posts: 33
Re: Save form contents to file.
« Reply #8 on: April 10, 2024, 04:42:24 pm »
It seems to have fixed itself.
text selection is back.

gjunk

  • New Member
  • *
  • Posts: 33
Re: Save form contents to file.
« Reply #9 on: April 11, 2024, 06:03:42 pm »
I have added this on 3 input forms and selected all of the input fields on each form to be saved.
It seems to be working.
Does it save when I exit the individual input forms or when I exit the program (main) form?
Is there a way to force a save?

gjunk

  • New Member
  • *
  • Posts: 33
Re: Save form contents to file.
« Reply #10 on: April 12, 2024, 05:07:38 pm »
It works.
I added the call to the close window procedure and it does a save on exit of the form.

Thank you.

 

TinyPortal © 2005-2018