Search code examples
lazarusfreepascal

Edit File on Click of a button in Lazarus


Like I posted in a previous thread I want to create a little program which edits one line in a .ini file. Now I have implemented a button but don't further. I basically want to implement the following scenario:

1) Click Button

2) Because of button click, program opens .txt/.ini file (in background) the file is located in the same folder

3) One word in text file is changed with new word

4) file gets saved

5) message pops up

procedure TLauncher.ButtonClick(Sender: TObject);

    var

    begin

     ShowMessage('.Ini-File was edited')
    end;

Solution

  • That's simple to do, if you split what you want to do into procedures that only do one thing each.

    Assume your form has a string variable IniFileName which you initialize however you want, e.g. using a TOpenDialog. Then you can have

    procedure TForm1.LoadIni;
    begin
      Memo1.Lines.LoadfromFile(IniFileName);
    end;
    
    procedure TForm1.SaveIni;
    begin
      Memo1.Lines.SaveToFile(IniFileName);
    end;
    
    procedure TForm1.Button1Click;
    begin
       if OpenDialog1.Execute then begin
         IniFileName := OpenDialog1.FileName;
         LoadIni;
       end;
    end;
    
    procedure TForm1.Button2Click;
    begin
       SaveIni;
       ShowMessage(IniFileName + ' saved to disk');
    end;