Recent

Author Topic: saving a few components values at once to one file.  (Read 5319 times)

Jakub2016

  • New member
  • *
  • Posts: 7
saving a few components values at once to one file.
« on: September 29, 2016, 09:15:56 pm »
Hello,

Gentlemen's, I need an advise ...

I have a few components on a form, edit's , check boxes , my quetions is , how to save the data from all of them at once to a single file , for example *.dat.
Is that possible ? I have a User Profile form and I need to save it somehow to review it in the future..
Any advices ... ?

Should I use *ini file to save values ? or tregistry ?


Regards
« Last Edit: September 29, 2016, 09:41:20 pm by Jakub2016 »

Handoko

  • Hero Member
  • *****
  • Posts: 5150
  • My goal: build my own game engine using Lazarus
Re: saving a few components values at once to one file.
« Reply #1 on: September 30, 2016, 05:03:40 am »
Both using TIniFile and TRegistry are okay. But I suggest you to use TIniFile, because TRegistry is for Windows only, you will have problem if you want to port it to other OSes in the future.

TIniFile documentation:
http://www.freepascal.org/docs-html/fcl/inifiles/tinifile.html

TIniFile tutorial:
http://wiki.freepascal.org/Using_INI_Files

TRegistry tutorial:
http://wiki.freepascal.org/fcl-registry

Edson

  • Hero Member
  • *****
  • Posts: 1302
Re: saving a few components values at once to one file.
« Reply #2 on: September 30, 2016, 06:51:16 am »
You can use mi library: https://github.com/t-edson/MiConfig
It can save the content of several components to disk (in INI or XML) and return the values from disk, easily.
Lazarus 2.2.6 - FPC 3.2.2 - x86_64-win64 on Windows 10

Thaddy

  • Hero Member
  • *****
  • Posts: 14364
  • Sensorship about opinions does not belong here.
Re: saving a few components values at once to one file.
« Reply #3 on: September 30, 2016, 06:59:43 am »
You seem to forget that published component properties are already streamable by default.
You can choose any format you want, but I would suggest simply to always use the build-in functionality to store and load properties at once.
e.g. readcomponentresfile,writecomponentresfile, TFiler, Treader, Twriter.

See http://wiki.freepascal.org/Streaming_components

That is the recommended way to save and load properties. There is absolutely no extra value in extra libraries, since the deafult mechanism is so easy to use and tried and tested.

The easiest way to use this on a higher level is to use the TStream.ReadComponent and TStream.WriteComponent methods on a filestream.
« Last Edit: September 30, 2016, 08:36:11 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

paweld

  • Hero Member
  • *****
  • Posts: 991
Best regards / Pozdrawiam
paweld

Jakub2016

  • New member
  • *
  • Posts: 7
Re: saving a few components values at once to one file.
« Reply #5 on: September 30, 2016, 04:28:40 pm »
Wow !

Thanks for the answers ! I am going to study it right now ...

Handoko

  • Hero Member
  • *****
  • Posts: 5150
  • My goal: build my own game engine using Lazarus
Re: saving a few components values at once to one file.
« Reply #6 on: September 30, 2016, 05:04:18 pm »
To improve your programming skills, you should study all of them. But I recommend you to use Thaddy's suggestion, it has many advantages compare to the rest. I didn't suggest TStream initially because I'm familiar with TIniFile (I used it a lot).

Edson

  • Hero Member
  • *****
  • Posts: 1302
Re: saving a few components values at once to one file.
« Reply #7 on: September 30, 2016, 06:11:22 pm »
You seem to forget that published component properties are already streamable by default.
No. I don't forget  :D. My solution is not oriented to save components properties directly to disk. It links components to variables and disk:

Components <-> Variables <-> Disk.

It's because, a common application, doesn't use components like containers. It stores his model in variables or properties.  Then, the components are only used when you need to edit his properties in dialogs (and there are some properties that are never edited in components).
« Last Edit: September 30, 2016, 06:15:53 pm by Edson »
Lazarus 2.2.6 - FPC 3.2.2 - x86_64-win64 on Windows 10

Jakub2016

  • New member
  • *
  • Posts: 7
Re: saving a few components values at once to one file.
« Reply #8 on: September 30, 2016, 07:21:48 pm »

I have used TXMLPropStorage , I am familiar with xml a bit already so I tried this method as first.
But I occurred some problems on the way , it looks like during the compilation time Lazarus is giving all the var's that have been set inside the project options, which is object inspector. The TstoredValue works only with true or false , or I can't set it neutral ( I don't know how to ), so everytime I open my application by Lazarus IDE ,it assign built in values, but if I open it from disk it works fine.

Thanks for Advices , I think I will use Tregistry to store values of form appearance rather than someone's profile content.

Regards

 

TinyPortal © 2005-2018