如何保存和加载我的 svTree 数据结构?

发布于 2024-12-04 02:44:14 字数 1118 浏览 2 评论 0原文

我正在尝试使用 VirtualStringTree 组件实现一个简单的联系人管理器。我将其设置为看起来像一个列表视图组件,只有三列,全部包含文本:

Layout

对于数据结构,我正在使用 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:

Layout

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暗喜 2024-12-11 02:44:14

您需要为您的 TSVTree 设置 OnLoadNode 和 OnSaveNode 过程并在此处实现您的逻辑。您可以查看 Demos 文件夹中的 Project2。例如:

uses
  uSvHelpers;

MyTree.OnSaveNode := DoSave;
MyTree.OnLoadNode := DoLoad;

procedure TForm1.DoLoad(Sender: TSVTree<TMainData>; Node: TSVTreeNode<TMainData>; Stream: TStream);
var
  obj: TMainData;
begin
  //
  if Assigned(Node) then
  begin
    //read from stream
    //read in correct order
    obj.Name := Stream.AsString;
    obj.Email := Stream.AsString;
    obj.Password := Stream.AsString;
    Node.FValue := obj;
  end;
end;

procedure TForm1.DoSave(Sender: TSVTree<TMainData>; Node: TSVTreeNode<TMainData>; Stream: TStream);
begin
  if Assigned(Node) then
  begin
    //read from stream
    Stream.WriteString(Node.FValue.Name);
    Stream.WriteString(Node.FValue.Email);
    Stream.WriteString(Node.FValue.Password);
  end;
end;

之后您只需调用 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.:

uses
  uSvHelpers;

MyTree.OnSaveNode := DoSave;
MyTree.OnLoadNode := DoLoad;

procedure TForm1.DoLoad(Sender: TSVTree<TMainData>; Node: TSVTreeNode<TMainData>; Stream: TStream);
var
  obj: TMainData;
begin
  //
  if Assigned(Node) then
  begin
    //read from stream
    //read in correct order
    obj.Name := Stream.AsString;
    obj.Email := Stream.AsString;
    obj.Password := Stream.AsString;
    Node.FValue := obj;
  end;
end;

procedure TForm1.DoSave(Sender: TSVTree<TMainData>; Node: TSVTreeNode<TMainData>; Stream: TStream);
begin
  if Assigned(Node) then
  begin
    //read from stream
    Stream.WriteString(Node.FValue.Name);
    Stream.WriteString(Node.FValue.Email);
    Stream.WriteString(Node.FValue.Password);
  end;
end;

After that you can just call MyTree.SaveToFile('C:/Test.dat') or MyTree.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.

天煞孤星 2024-12-11 02:44:14

看起来您需要实现 OnSaveNode 和 OnLoadNode 事件:

procedure TForm.VTLoadNode(Sender: TBaseVirtualTree;
  Node: PVirtualNode; Stream: TStream);
begin
  // Load Node Data record from the stream
end;

procedure TForm.VTSaveNode(Sender: TBaseVirtualTree;
  Node: PVirtualNode; Stream: TStream);
begin
  // Save Node Data record to the stream
end;

Looks like you need to implement the OnSaveNode and OnLoadNode events:

procedure TForm.VTLoadNode(Sender: TBaseVirtualTree;
  Node: PVirtualNode; Stream: TStream);
begin
  // Load Node Data record from the stream
end;

procedure TForm.VTSaveNode(Sender: TBaseVirtualTree;
  Node: PVirtualNode; Stream: TStream);
begin
  // Save Node Data record to the stream
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文