如何保存和加载我的 svTree 数据结构?
我正在尝试使用 VirtualStringTree 组件实现一个简单的联系人管理器。我将其设置为看起来像一个列表视图组件,只有三列,全部包含文本:
对于数据结构,我正在使用 Linas 的 svTree,这在另一个 Stack Overflow 问题中提到过。
我已经声明了这样的记录:
type
TMainData = record
Name, Email, Password: string;
end;
在表单的 OnCreate 中,我有这样的:
procedure TForm1.FormCreate(Sender: TObject);
begin
MyTree := TSVTree<TMainData>.Create(False);
MyTree.VirtualTree := vst1;
end;
我正在添加来自 TEdits 的数据,如下所示:
procedure TForm1.BuildStructure;
var
svNode: TSVTreeNode<TMainData>;
Data: TMainData;
begin
MyTree.BeginUpdate;
try
Data.Name := edtname.Text;
Data.Email := edtEmail.Text;
Data.Password := edtPassword.Text;
svNode := MyTree.AddChild(nil, Data);
finally
MyTree.EndUpdate;
end;
Label1.Caption := 'Count: '+IntToStr(MyTree.TotalCount);
end;
如何将其保存到要加载回的流或文件中?我尝试过使用 MyTree.SaveToFile('C:/Test.dat')
和 MyTree.LoadFromFile('C:/Test.dat')
,但是当它加载时后面的树视图不包含任何数据,只有一个空白行。
I am trying to implement a simple contact manager using the VirtualStringTree component. I have it set up to look like a list-view component with only three columns that will all contain text:
For the data structure, I am using svTree by Linas, which was mentioned in another Stack Overflow question.
I have declared a record like this:
type
TMainData = record
Name, Email, Password: string;
end;
In the form's OnCreate I have this:
procedure TForm1.FormCreate(Sender: TObject);
begin
MyTree := TSVTree<TMainData>.Create(False);
MyTree.VirtualTree := vst1;
end;
I am adding the data from TEdits like this:
procedure TForm1.BuildStructure;
var
svNode: TSVTreeNode<TMainData>;
Data: TMainData;
begin
MyTree.BeginUpdate;
try
Data.Name := edtname.Text;
Data.Email := edtEmail.Text;
Data.Password := edtPassword.Text;
svNode := MyTree.AddChild(nil, Data);
finally
MyTree.EndUpdate;
end;
Label1.Caption := 'Count: '+IntToStr(MyTree.TotalCount);
end;
How can I save this into a stream or a file to be loaded back? I have tried using MyTree.SaveToFile('C:/Test.dat')
and MyTree.LoadFromFile('C:/Test.dat')
, but when it's loaded back the tree view contains no data, only a blank row.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要为您的 TSVTree 设置 OnLoadNode 和 OnSaveNode 过程并在此处实现您的逻辑。您可以查看 Demos 文件夹中的 Project2。例如:
之后您只需调用
MyTree.SaveToFile('C:/Test.dat')
或MyTree.LoadFromFile('C:/Test.dat')
即可。在我的演示和本示例中,我使用了另一个单元 (uSvHelpers),它实现了 TStream 助手以提供更多 OO 流支持。您当然可以使用自己的方式将数据信息写入流。You need to set OnLoadNode and OnSaveNode procedures for your TSVTree and implement your logic here. You can look at Project2 in the Demos folder. E.g.:
After that you can just call
MyTree.SaveToFile('C:/Test.dat')
orMyTree.LoadFromFile('C:/Test.dat')
. In my demo and this example i've used another unit (uSvHelpers) which implements TStream helper for more OO stream support. You can of course use your own way to write your data information to stream.看起来您需要实现 OnSaveNode 和 OnLoadNode 事件:
Looks like you need to implement the OnSaveNode and OnLoadNode events: