Charger/Enregistrer un Treeview dans un ini

procedure TreeToIni(Tree: TTreeView; INI: TIniFile; Section: string);
var
n: Integer;
MS: TMemoryStream;
tTv: TStringList;
Msg: string;
begin
tTv := TStringList.Create;
MS := TMemoryStream.Create;
try
Tree.SaveToStream(MS);
MS.Position := 0;
tTv.LoadFromStream(MS);
INI.EraseSection(Section);
for n := 0 to tTv.Count - 1 do
INI.WriteString(Section, 'Node' + IntToStr(n), StringReplace(tTv[n], #9,
'#', [rfReplaceAll]));
finally
tTv.Free;
MS.Free;
end;
end;

procedure TreeFromIni(Tree: TTreeView; INI: TIniFile; Section: string;
Expand: Boolean);
var
n: Integer;
MS: TMemoryStream;
tTv: TStringList;
Msg: string;
begin
tTv := TStringList.Create;
MS := TMemoryStream.Create;
try
INI.ReadSection(Section, tTv);
for n := 0 to tTv.Count - 1 do
tTv[n] := StringReplace(INI.ReadString(Section, tTv[n], ''), '#', #9,
[rfReplaceAll]);
tTv.SaveToStream(MS);
MS.Position := 0;
Tree.LoadFromStream(MS);
if (Expand = True) and (Tree.Items.Count > 0) then
Tree.Items[0].Expand(True);
finally
tTv.Free;
MS.Free;
end;
end;

procedure TF_Main.Button2Click(Sender: TObject);
var
iniFile: TiniFile;
begin
iniFile := TiniFile.Create('c: est.ini');
try
TreeToIni(TreeAlbum, iniFile, 'ma Section');
finally
iniFile.free;
end;
end;

procedure TF_Main.Button3Click(Sender: TObject);
var
iniFile: TiniFile;
begin
iniFile := TiniFile.Create('c: est.ini');
try
TreeFromIni(TreeAlbum, iniFile, 'ma Section', true);
finally
iniFile.free;
end;
end;
Ajouter un commentaire
Pseudo
Adresse email
Site internet (optionnel)
Votre commentaire
Fiche de l'article
Mise a jour02/05/2005
VisualisationVu 1890 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