Link to home
Start Free TrialLog in
Avatar of pr2501
pr2501

asked on

Write into text file from TMemo and reading back again

With next code i write by clicking into memo2 first date(day,year,...) and then i continue with text.
Next time i reopen my App text wrote last time appears into memo1.
So, i have some kind of notes.

The code below with two TMemos works fine but,...
I thought it would be more appropriate if i had only one tmemo.
How  can be  done that.

// ading Date(day,year,...)
procedure TForm2.Memo2Click(Sender: TObject);
begin
   Memo2.Lines.Add(DateToStr(Date));
   Memo2.SelStart := Length(Memo2.Text);
end;
 //    write  note from memo2  to text file
procedure TForm2.AddMemo(NomeFile:string);
    var
   TextText: textfile;
   begin
    AssignFile( TextText,NomeFile);
    Append( TextText);
    WriteLn( TextText,Memo2.Text);
    WriteLn( TextText,'');
    CloseFile( TextText);
end;
 
//read note to memo1 from text file
procedure TForm2.ReadMemo(NomeFile:string);
begin
  Memo1.Lines.LoadFromFile(NomeFile);
  Memo1.SelStart := Length(Memo1.Lines.Text)-1;
  Memo1.SelLength := 1;
  Memo1.SetFocus;
  end;
 
  // call  read note to memo1
procedure TForm2.FormActivate(Sender: TObject);
 begin
      ReadMemo('C:\mix.txt');
 end;
 
 // call write from memo2 
procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
  begin
    AddMemo('C:\mix.txt');
end;

Open in new window

Avatar of tammoz
tammoz

Hi
to save the memo data to a file you can use the
Memo1.Lines.SaveToFile('C:\mix.txt');

and then to load them you can use:
Memo1.Lines.LoadFromFile('C:\mix.txt');
Avatar of pr2501

ASKER



  I'm doing that.
  but in my way i must have two TMemos (one for write and other wor write).
 
 Because with one:

 With one TMemo (write from it to faile and then read back)
it adds text again and again:

Text= text +text

Next reopening:
Text=(text+text)+text

and so on.
SOLUTION
Avatar of tammoz
tammoz

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial