Let's say I have two text files (.txt) and I have a form with one TMemo Component on it. What would be the best way to quickly load both text files into the same Memo?
Use a TStringList
to load each file and then use the AddStrings
method to transfer the contents to the memo.
var
Tmp: TStringList;
...
Memo1.Lines.BeginUpdate;
try
Memo1.Lines.Clear;
Tmp := TStringList.Create;
try
Tmp.LoadFromFile(FileName1);
Memo1.Lines.AddStrings(Tmp);
Tmp.LoadFromFile(FileName2);
Memo1.Lines.AddStrings(Tmp);
finally
Tmp.Free;
end;
finally
Memo1.Lines.EndUpdate;
end;
In fact, this can easily be generalised to a potentially useful method like this:
procedure AppendMultipleTextFiles(Dest: TStrings; const FileNames: array of string);
var
FileName: string;
Tmp: TStringList;
begin
Dest.BeginUpdate;
try
Tmp := TStringList.Create;
try
for FileName in FileNames do
begin
Tmp.LoadFromFile(FileName);
Dest.AddStrings(Tmp);
end;
finally
Tmp.Free;
end;
finally
Dest.EndUpdate;
end;
end;
You could then use the method like so:
Memo1.Lines.Clear;
AppendMultipleTextFiles(Memo1.Lines, [FileName1, FileName2]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With