Title: Logging a Memo's text to a TextFile or LogFile
Question: Learn how to log text from a Memo (or RichEdit etc) into a log file (or TextFile). Only new Memo data is logged into the file.
Answer:
This is one method for logging new data of a Memo (or RichEdit etc) into a text file or log file.
Create a variable in the private section of your form, LineCount:
private
{ Private declarations }
LineCount: integer;
public
{ Public declarations }
end;
OnFormCreate, set this LineCount to 0:
procedure TForm1.FormCreate(Sender: TObject);
begin
LineCount := 0;
end;
This example uses a Memo component, so in your Memo's OnChange event, do this:
procedure TForm1.Memo1Change(Sender: TObject);
var
F: TextFile;
LinesToLog, i: integer;
begin
if Memo1.Lines.Count LineCount then begin
LinesToLog := Memo1.Lines.Count - LineCount;
end;
LineCount := Memo1.Lines.Count;
// log code
AssignFile(F, 'c:\test.log');
if FileExists('c:\test.log') then Append(F) else Rewrite(F);
for i := LinesToLog downto 1 do begin
WriteLn(F, Memo1.Lines[((LineCount)-i)]);
end;
CloseFile(F);
end;
This will log any new lines added to a Memo, but this will not work if you are typing directly into a Memo - It will log too much, or cause errors. It will only work for text placed into the Memo, like in a chat program, or some other system.
Also, the size of the lines in the log file will match those directly of the Memo. I.E: If something could not fit on one line in the Memo and had to move onto two lines, this will show in the log file.
I would appreciate any other methods for achieving this, or any methods to get the Memo text which wouldn't fit on one line, to log to one line (or as far as possible until WordWrap) in the log file.
Robert.