How to select all root or all child nodes in VirtualStringTree?

一曲冷凌霜 提交于 2020-01-03 15:15:53

问题


I would like to select either all root nodes or all child nodes (not all nodes in a VirtualTreeView).
I've tried to use this code to select all root nodes:

procedure SelectAllRoots;
var
  Node: PVirtualNode;
begin
  Form1.VirtualStringTree1.BeginUpdate;
  Node := Form1.VirtualStringTree1.GetFirst;
  while True do 
  begin
    if Node = nil then 
      Break;
    if not (vsSelected in Node.States) then
      Node.States := Node.States + [vsSelected];
    Node := Form1.VirtualStringTree1.GetNext(Node);
  end;
  Form1.VirtualStringTree1.EndUpdate;
end;

I can tell there's a small glitch. The selection is either incomplete or gets stuck. What am I doing wrong ?

Edit:
I use MultiSelection.


回答1:


1. Select all root nodes:

To select all root nodes, you can use the following procedure:

procedure SelectRootNodes(AVirtualTree: TBaseVirtualTree);
var
  Node: PVirtualNode;
begin
  AVirtualTree.BeginUpdate;
  try
    Node := AVirtualTree.GetFirst;
    while Assigned(Node) do
    begin
      AVirtualTree.Selected[Node] := True;
      Node := AVirtualTree.GetNextSibling(Node);
    end;
  finally
    AVirtualTree.EndUpdate;
  end;
end;

2. Select all child nodes:

To select all child nodes, level independent, you need to use recursive function like this:

procedure SelectChildNodes(AVirtualTree: TBaseVirtualTree);
var
  Node: PVirtualNode;

  procedure SelectSubNodes(ANode: PVirtualNode);
  var
    SubNode: PVirtualNode;
  begin
    SubNode := AVirtualTree.GetFirstChild(ANode);
    while Assigned(SubNode) do
    begin
      SelectSubNodes(SubNode);
      AVirtualTree.Selected[SubNode] := True;
      SubNode := AVirtualTree.GetNextSibling(SubNode);
    end;
  end;

begin
  AVirtualTree.BeginUpdate;
  try
    Node := AVirtualTree.GetFirst;
    while Assigned(Node) do
    begin
      SelectSubNodes(Node);
      Node := AVirtualTree.GetNextSibling(Node);
    end;
  finally
    AVirtualTree.EndUpdate;
  end;
end;


来源:https://stackoverflow.com/questions/12142258/how-to-select-all-root-or-all-child-nodes-in-virtualstringtree

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!