2008. március 7., péntek

Save and load the state of a TMenuItem to/ from a TIniFile


Problem/Question/Abstract:

How to save and load the state of a TMenuItem to/ from a TIniFile

Answer:

uses
  IniFiles;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
  ini: TIniFile;
begin
  {Save the checked state of each menu item when the form closes}
  Ini := TIniFile.Create('mysettings.ini');
  Ini.WriteBool('Settings', 'MenuItem1Checked', MenuItem1.Checked);
  Ini.WriteBool('Settings', 'MenuItem2Checked', MenuItem2.Checked);
  Ini.WriteBool('Settings', 'MenuItem2Checked', MenuItem2.Checked);
  Ini.Free;
end;

procedure TForm1.FormShow(Sender: TObject);
var
  ini: TIniFile;
begin
  {Reload the checked state of each menu item when the form opens}
  Ini := TIniFile.Create('mysettings.ini');
  MenuItem1.Checked := Ini.ReadBool('Settings', 'MenuItem1Checked', False);
  MenuItem2.Checked := Ini.ReadBool('Settings', 'MenuItem2Checked', False);
  MenuItem3.Checked := Ini.ReadBool('Settings', 'MenuItem3Checked', False);
  Ini.Free;
end;

Nincsenek megjegyzések:

Megjegyzés küldése