Author: Roni Havas
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:
1 2 procedure TForm1.BackupTreeView(MyTree: TTReeView; ToFile: string);
3 begin4 with TFileStream.Create(ToFile, fmCreate) do5 try6 WriteComponent(MyTree);
7 finally8 Free;
9 end;
10 end;
11 12 procedure TForm1.RestoreTreeView(MyTree: TTReeView; FromFile: string);
13 begin14 with TFileStream.Create(FromFile, fmOpenRead) do15 try16 MyTree.Clear;
17 ReadComponent(MyTree);
18 finally19 Free;
20 end;
21 22 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.