• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 271
  • Last Modified:

Saving form data to an inifile

Hello,
How would you save form information to an infile. For example if a checkbox was checked by a user or a menu item was checked.When the program is restarted the above conditions are restored into the form by reading a inifile.
0
frw
Asked:
frw
  • 3
  • 3
1 Solution
 
classmateCommented:
Use a TIniFile object to create and access an Inifile.

Further answer depends on how automatic you want the functionality to be. If it's possible to define a limited collection of properties to save and load, you can define an ID for each of them.

Classmate.
0
 
classmateCommented:
Use e.g. TCustomIniFile.WriteBool to save the TCheckBox.Checked property.

Example:
  MyIniFile.WriteBool ('FormProperties', CheckBox1.Name, MyCheckBox.Checked);

This saves the value of MyCheckBox.Checked under the section 'FormProperties' at the ident CheckBox1.Name.

In e.g. your OnShow or onCreate event handlers of your form, write a corresponding ReadBool call.


classmate.
0
 
classmateCommented:
Sorry, CheckBox1=MyCheckBox.... :-(
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
intheCommented:
Hi
well this example is for showing a splasscreen on startup if a checkbox is checked but principal is the same:

uses inifiles;

TForm1 = class(TForm)
CheckBox1: TCheckBox;
{etc.}
private
IniFile: TIniFile;
ShowSplash: boolean;
end;
 
procedure FormCreate(Sender: TObject);
begin
IniFile:=TIniFile.Create('c:\MyFile.ini');
ShowSplash := IniFile.ReadBool('Splash', 'Show', True);
CheckBox1.Checked := ShowSplash;
if ShowSplash then
  SplashForm := TSplashForm.Create(Application);
end;
 
procedure FormClose(Sender: TObject);
begin
ShowSplash := CheckBox1.Checked;
IniFile.WriteBool('Splash', 'Show', ShowSplash);
IniFile.Free
end;
 
end.
 


it is probably beter nowadays to use the registry to save these settings.
if you need i can give an example of saving the forms settings to the registry instead.
Regards Barry
0
 
intheCommented:
sorry classmate posted without pressing refresh :-(
0
 
frwAuthor Commented:
Could you send me the Registry example.
Thankyou.
batt@netrover.com
0
 
intheCommented:
ok cheers,
have emailed a demo of saving the settings in the registry.
Regards Barry
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now