Author: Tomas Rutkauskas
How to save and load the state of a TMenuItem to/ from a TIniFile
Answer:
1 uses2 IniFiles;
3 4 procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
5 var6 ini: TIniFile;
7 begin8 {Save the checked state of each menu item when the form closes}9 Ini := TIniFile.Create('mysettings.ini');
10 Ini.WriteBool('Settings', 'MenuItem1Checked', MenuItem1.Checked);
11 Ini.WriteBool('Settings', 'MenuItem2Checked', MenuItem2.Checked);
12 Ini.WriteBool('Settings', 'MenuItem2Checked', MenuItem2.Checked);
13 Ini.Free;
14 end;
15 16 procedure TForm1.FormShow(Sender: TObject);
17 var18 ini: TIniFile;
19 begin20 {Reload the checked state of each menu item when the form opens}21 Ini := TIniFile.Create('mysettings.ini');
22 MenuItem1.Checked := Ini.ReadBool('Settings', 'MenuItem1Checked', False);
23 MenuItem2.Checked := Ini.ReadBool('Settings', 'MenuItem2Checked', False);
24 MenuItem3.Checked := Ini.ReadBool('Settings', 'MenuItem3Checked', False);
25 Ini.Free;
26 end;