This is a very condensed version of a guide I put on the web to help people with using ini files with Lazarus, written June 2022, after years of doing such things. I commend it to you if you want explanations of the details of using ini files.
The page you are reading now is to help someone who "knows all about" using ini files when he or she is adding them to a app's code. A checklist of the necessary ingredients.
I'll try to present the "ingredients" in an order that could be followed in building the app.
The .ini file: Start with a reasonably clear idea of what you want to control via the ini file. Decide what your Sections and Keys are going to be. Have at least a "skeleton" .ini file available for development work.
Uses IniFiles: Remember to put "IniFiles" in the uses clause, near the top of the app.
Inifile name: Ordianrily, I'd put the name of the inifile in a global constant. You can waste a lot of time by accidentally having the app looking in two places for inifile matters. And if you decide to change the name of the inifile, establishing it once with that global constant, and using the constant any time you need the inifile's name will have saved you nuisance and uncertainty. (Did you find and change EVERY reference to the inifile's name?)
Start with using an ini file that is NOT changed by the app. In some cases you will need to do the ini file things in FormActivate. Therefor, I'd try to do all of them there. There may be times when you have to visit the ini file during FormCreate and during FormActivate.
Here is some code illustrating various bits of what will happen during the read of the ini file...
procedure Tldn150f1.FormActivate(Sender: TObject); var dfIniFile:TIniFile;//"df" in name from "datafile". Could be anything. begin //Heart of reading from ini file dfIniFile:=TIniFile.Create(skIniFileName); with dfIniFile do begin eFirstTerm.text:=readstring('Terms','FirstTerm','999'); eSecondTerm.text:=readstring('Terms','SecondTerm','998'); with ldn150f1 do begin left:=readinteger('AppWindow','left',30); top:=readinteger('AppWindow','top',10); width:=readinteger('AppWindow','width',350); height:=readinteger('AppWindow','height',120); end;//with ldn150f1 end;//with dfInifile... dfIniFile.Free; //End of "Heart of...." //There may be other code here, or before the ini file code. end;//FromActivate
When that much is in place, you're nearly done.
Remember that if you exit with application.terminate, or by using the red square "stop app" while running the code under the IDE, you will bypass what the following provides for.
(Use "close" to shut the app down by, say, clicking a "Quit" button.)
Here is some code illustrating various bits of what might happen during writing updates back to the ini file. The code goes in the FormClose handler. i would be inclined to "wrap" the code for saving the app's state to the ini file in its own procedure, called "SaveState" in the example below. SaveState would be called from the FormClose handler.
procedure Tldn150f1.SaveState; var dfIniFile:TIniFile;//"df" in name from "datafile". Could be anything. begin //Heart of writing to ini file dfIniFile:=TIniFile.Create(skIniFileName); with dfIniFile do begin writestring('Terms','FirstTerm',eFirstTerm.text); writestring('Terms','SecondTerm',eSecondTerm.text); with ldn150f1 do begin writeinteger('AppWindow','left',left); writeinteger('AppWindow','top',top); writeinteger('AppWindow','width',width); writeinteger('AppWindow','height',height); end;//with ldn150f1 end;//with dfInifile dfIniFile.Free; //End of "Heart of...." end; //SaveState
And that's it for this "quick list"/ "summary" version of my guide to using ini files with Lazarus.
Please get in touch if you discover flaws in this page. Please mention the page's URL. (wywtk.com/lut/ldn150inifiles/ldn150_quicklist.htm).
If you found this of interest, please mention in forums, give it a Facebook "like", Google "Plus", or whatever. If you want more of this stuff, help!? There's not much point in me writing these things, if no one feels they are of any use.
Unlike the clever Google search engine, this one merely looks for the words you type, so....
* Spell them properly.
* Don't bother with "How do I get rich?" That will merely return pages with "how", "do", "I"....
Please also note that I have three other sites, and that this search will not include them. They have their own search buttons.
My SheepdogSoftware.co.uk site, where you'll find my main homepage. It has links for other areas, such as education, programming, investing.
My SheepdogGuides.com site.
My site at Arunet.
Page has been tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org. It passes in some important ways, but still needs work to fully meet HTML 5 expectations. (If your browser hides your history, you may have to put the page's URL into the validator by hand. Check what page the validator looked at before becoming alarmed by a "not found" or "wrong doctype".)
....... P a g e . . . E n d s .....