Enregistrer/charger plusieurs composants dans un fichier

procedure SaveEditcontrols( const FileName: string; const Controls: array of TCustomEdit);
var
fs: TFilestream;
writer: TWriter;
i: Integer;
ss: TStringstream;
begin
fs := TFilestream.Create(FileName, fmCreate);
try
writer := TWriter.Create(fs, 4096);
try
for i := Low(Controls) to High(Controls) do
if Controls[i] is TCustomRichedit then
begin
ss := TStringstream.Create(EmptyStr);
try
with TRichedit(Controls[i]) do
begin
Plaintext := False;
Lines.SaveToStream(ss);
end;
writer.WriteString(ss.Datastring);
finally
ss.Free;
end;
end
else
writer.WriteString(Controls[i].Text);
finally
writer.Free;
end;
finally
fs.Free;
end;
end;

procedure LoadEditcontrols( const FileName: string; const Controls: array of TCustomEdit);
var
fs: TFilestream;
reader: Treader;
i: Integer;
ss: TStringstream;
begin
fs := TFilestream.Create(FileName, fmOpenread or fmShareDenyWrite);
try
reader := Treader.Create(fs, 4096);
try
for i := Low(Controls) to High(Controls) do
if Controls[i] is TCustomRichedit then
begin
ss := TStringstream.Create(reader.ReadString);
try
with TRichedit(Controls[i]) do
begin
Plaintext := False;
Lines.LoadfromStream(ss);
end;
finally
ss.Free;
end;
end
else
Controls[i].Text := reader.ReadString;
finally
reader.Free;
end;
finally
fs.Free;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
SaveEditControls('C: emp emp.dat',
[richedit1, richedit2, edit1, edit2, edit3]);
end;
Ajouter un commentaire
Pseudo
Adresse email
Site internet (optionnel)
Votre commentaire
Fiche de l'article
Mise a jour05/10/2002
VisualisationVu 1515 fois
PublicInternaute zz
CategorieDelphi - Trucs et astuces - Objet
Auteur de l'article
SwissDelphiCenter SwissDelphiCenter
connecté le 02/11/2011
329 articles dans la section
Contacter l'auteur