[SOLVED] Save user settings lazarus / 21-General ---------------------------------------------------------------------- Hansvb · 2021-07-17 09:24 Hi, I haven't been using Lazarus for a long time. When I want to save user settings I used to do that in an ini file. Is this still the way to go or are there better alternatives? Greetings ---------------------------------------------------------------------- Fantablup · 2021-07-17 09:31 I also used to do that before. Also used to save in registry. But, now i always save these things as JSON. It is much more structured and easier. You have to learn the JSON functions though. ---------------------------------------------------------------------- Hansvb · 2021-07-17 09:55 Do you know a tutorial or small sample how to do that? ---------------------------------------------------------------------- Bart · 2021-07-17 09:57 Having you settings in a human readable format (INI, JSON, XML or whatever) has the advantage that it is easy to fix (if something went wrong), even for novice users. (Most Windows users never ever opened the registry, or even know that it exists, als f*cking up the registry (once I accidentally removed all subkeys from Computer\HKEY_LOCAL_MACHINE\SOFTWARE ) can be unfixable.) So, I tend not to use the Windows registry at all for my program settings. If you don't need things like multi line values, then Ini files are perfectly OK, and easier to handle and understand than JSON or XML. So, it all depends on your use case. Bart ---------------------------------------------------------------------- Fantablup · 2021-07-17 10:13 Quote from: Hansvb on July 17, 2021, 11:55:35 amDo you know a tutorial or small sample how to do that? Sorry, i have no tutorials you can use. You have to search for examples. I guess it takes a day or two to get the understanding of it. So it is some work learning it. But, as the above said. It is also perfectly OK to use INI if you only need a simple thing. But JSON is good for categorizing users, and have all settings in one node for each user. ---------------------------------------------------------------------- wp · 2021-07-17 13:28 Quote from: Hansvb on July 17, 2021, 11:24:18 amI haven't been using Lazarus for a long time. When I want to save user settings I used to do that in an ini file. Is this still the way to go or are there better alternatives? It depends on what you define as "better"... If "better" is avoiding to write code you should try to learn how to use the PropStorage components on palette "Misc": TXMLPropStorage, TIniPropStorage, TJSONPropStorage. Depending on the component that you select you write an xml, ini, or json config file. They cooperate with the SessionProperties of the form where you can define which properties of the form or of any component on the form will be stored (and, of course, retrieved when the application restarts the next time). There's a wiki tutorial on https://wiki.lazarus.freepascal.org/TXMLPropStorage for XMLPropStorage, but the others work in the same way. I personally, however, do not considers this as an adequate way to separate code and gui. But for "quick-and-dirty" programs the PropStorage components can be a real time-saver. ---------------------------------------------------------------------- Renat.Su · 2021-07-17 14:50 Quote from: Hansvb on July 17, 2021, 11:24:18 amHi, I haven't been using Lazarus for a long time. When I want to save user settings I used to do that in an ini file. Is this still the way to go or are there better alternatives? Greetings Recently, I have been using serialization and deserialization in JSON more often. Here is an example of functions that load your any object in JSON and load from it https://gist.github.com/Al- Muhandis/1a4743e6447ee16696e6ef737a031be1 ---------------------------------------------------------------------- Hansvb · 2021-07-17 20:34 Thanks..., for now, I will use IniFiles. later I will take a look at JSON. ---------------------------------------------------------------------- alpine · 2021-07-17 21:23 Quote from: Hansvb on July 17, 2021, 10:34:03 pmThanks..., for now, I will use IniFiles. later I will take a look at JSON. Here is my small unit for simple ini configuration (attached). You can use it in a following way: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses  iniprops; type  TMyConfigObject = class(TIniFileProps)    FIntProp: Integer;    FBoolProp: Boolean;   published    property IntProp: Integer read FInitProp write FInitProp;    property BoolProp: Boolean read FBoolProp write FBoolProp;    ...  end;   // to create/load  MyConf := TMyConfigObject.Create('config.ini');  MyConf.LoadAllProps;   // to update  MyConf.IntProp := 5;  MyConf.BoolProp := False;  MyConf.StoreAllProps;   It loads/stores properties of type: Char, String, Integer, Int64, QWord, Bool, Float, Enumeration, Set. Of course, the LoadAllProps and StoreAllProps are declared virtual and you can override them to do some more complicated load/store actions. ---------------------------------------------------------------------- Gustavo 'Gus' Carreno · 2021-07-17 23:56 Hey Hansvb, Quote from: Hansvb on July 17, 2021, 10:34:03 pmThanks..., for now, I will use IniFiles. later I will take a look at JSON. I strongly advise you to have a good look at the T[XML|INI|JSON]PropStorage like @wp suggested. They take away a lot of the pain of loading and storing you values. They also contain an area that you can use separate from the App's components. This way you can safely save some custom settings like the ones you have on a Settings form/section. Attached is an Application that I've done as an example of a simple App with most of the boiler plate I do when starting a new application. It has a TJSONPropStorage for the prop storage. It has automated Windows and Linux exit short-cuts(I'm sorry but this is a pet peeve of mine ) It also has a TPairSplit thrown into the mix and, now, I can't remember why I put it there, LOL!! Hope this helps in any way. Cheers, Gus ---------------------------------------------------------------------- Edson · 2021-07-18 03:33 Just in case you need to move information in this directions: Disk(INI or XML) <---> Variables <---> Controls You can use my library: https://github.com/t-edson/MiConfig ---------------------------------------------------------------------- dbannon · 2021-07-18 06:58 I am a bit puzzled about the "not inifile" attitude.  It works and is certainly tried and tested. It works across any platform you may come across, if necessary, it can be hand edited and the Lazarus Ini file interface is very easy to use. I wonder just how complicated a User Settings config we need before the ini file that Hansvb is familiar with is inadequate ? Davo Yeah, you guessed it, tomboy-ng uses ini files ...... ---------------------------------------------------------------------- Hansvb · 2021-07-18 07:03 Thanks, i will look at the different options. I also looked at some old code of myself. (Using inifiles). That still works, which i find great to see. I just program as a hobby and i’m not verry good but like to do it. The last few years I used c# and the i saved settings in json format. I find it hard to find good examples in Pascal ---------------------------------------------------------------------- jcmontherock · 2021-07-24 10:03 Another way: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure Form1.SaveParms(fForm: TForm);var  MemoryStream: TMemoryStream;  FormRect:     TRect;begin  MemoryStream := TMemoryStream.Create;  FormRect := Bounds(fForm.Left, fForm.Top, fForm.Width, fForm.Height);  MemoryStream.Write(FormRect, SizeOf(FormRect));  MemoryStream.WriteAnsiString(lEditFile.Text);  MemoryStream.Position := 0;  sFilename := ExtractFilePath(ParamStr(0)) + ChangeFileExt({$I %FILE%}, '.parm');  MemoryStream.SaveToFile(sFilename);  MemoryStream.Free;end; procedure Form1.ReadParms(fForm: TForm);var  MemoryStream: TMemoryStream;  FormRect:     TRect;  sFilename:    String;begin  sFilename := ExtractFilePath(ParamStr(0)) + ChangeFileExt({$I %FILE%}, '.parm');  if FileExists(sFilename) = False then Exit;  MemoryStream := TMemoryStream.Create;  MemoryStream.LoadFromFile(sFilename);  MemoryStream.Position := 0;  MemoryStream.Read(FormRect, SizeOf(FormRect));  fForm.SetBounds(FormRect.Left, FormRect.Top, FormRect.Width, FormRect.Height);  lEditFile.Text := MemoryStream.ReadAnsiString;  MemoryStream.Free;end; ---------------------------------------------------------------------- trev · 2021-07-25 01:58 Quote from: dbannon on July 18, 2021, 08:58:37 amI am a bit puzzled about the "not inifile" attitude. Me too - I use INI files everywhere (FreeBSD, Linux, Windows) except macOS where I use the native solution.