Search code examples
delphisaving-data

How can I save the changes of my form on form close?


I want to save the changes made to the properties of my components on android when closing the form. How can I do this?


Solution

  • As people stated in the comments, this has to be done manually. For simple settings, it would be easy to use inifiles. For more advanced data saving you can use json or even sqlite.

    Just a note. The OnFormClose event doesn't really work that well on android. I suggest that when you make changes, that's when you save them, or alternatively, provide something like a "save config" button.

    Code example for saving ini on android:

    procedure SaveSettingString(Section, Name, Value: string);
    var
      ini: TIniFile;
    begin
      ini := TIniFile.Create(System.IOUtils.TPath.GetDocumentsPath + System.SysUtils.PathDelim + 'config.ini');
      try
        ini.WriteString(Section, Name, Value);
      finally
        ini.Free;
      end;
    end;
    

    Example for loading a string:

    function LoadSettingString(Section, Name, Value: string): string;
    var
      ini: TIniFile;
    begin
      ini := TIniFile.Create(System.IOUtils.TPath.GetDocumentsPath + System.SysUtils.PathDelim + 'config.ini');
      try
        Result := ini.ReadString(Section, Name, Value);
      finally
        ini.Free;
      end;
    end;
    

    When loading, what ever you set as the value, will be returned if that Name/Key does not exist.