Mirror

How to Backup and Restore the content of a TreeView (Views: 14)


Problem/Question/Abstract:

How can I backup (save) and the restore (load) the content of my TreeView to a file?

Answer:

Use the following two procedures to Backup and Restore the content of your TreeView:

procedure TForm1.BackupTreeView(MyTree: TTReeView; ToFile: string);
begin
  with TFileStream.Create(ToFile, fmCreate) do
  try
    WriteComponent(MyTree);
  finally
    Free;
  end;
end;

procedure TForm1.RestoreTreeView(MyTree: TTReeView; FromFile: string);
begin
  with TFileStream.Create(FromFile, fmOpenRead) do
  try
    MyTree.Clear;
    ReadComponent(MyTree);
  finally
    Free;
  end;

end;

This approach will not keep any data associated with the nodes, you need take care about that separately. The only thing it will do is preserve the tree structure and node names. You also will not be able to restore the treeview to any other component than original one (say to the other form) without risking to screw up everything.

<< Back to main page