How to know a node is root in Virtual TreeView?

倖福魔咒の 提交于 2021-01-27 07:27:46

问题


I am using Virtual Tree View. Is there a reliable way to know if a node is root or not?

I try to use

if not Assigned(Node.Parent) then
  Output('This is root')
else
  Output('This is not root')

But does not work.

I try to use

if Node = tvItems.RootNode then
  Output('This is root')
else
  Output('This is not root')

But does not work either.


回答1:


The ultimate root node in VTV (or VST) is a special invisible node, which acts as parent for all user created root nodes (nodes created with parent = nil). This special invisible node has by design its NextSibling and PrevSibling properties set to point to itself.

To detect whether a node is root node (in the sense of user created root) you can e.g. do:

procedure TForm16.tvItemsNodeClick(Sender: TBaseVirtualTree;
  const HitInfo: THitInfo);
begin
  if HitInfo.HitNode.Parent.NextSibling = HitInfo.HitNode.Parent then
    Caption := 'Root node'
  else
    Caption := 'Not root node';
end;

Alternatively, as OP commented, and without using internal implementation details:

procedure TForm16.tvItemsNodeClick(Sender: TBaseVirtualTree;
  const HitInfo: THitInfo);
begin
  if HitInfo.HitNode.Parent = Sender.RootNode then
    Caption := 'Root node'
  else
    Caption := 'Not root node';
end;

Ref: TBaseVirtualTree.RootNode Property (in Help)



来源:https://stackoverflow.com/questions/58057439/how-to-know-a-node-is-root-in-virtual-treeview

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