如何只扩展到树视图的第二层 [英] how to expand only to the second level of the treeview

查看:86
本文介绍了如何只扩展到树视图的第二层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如。


    - link 1
    -- link 1.1
    --- link 1.1.1(only)
    ---- link 1.1.1.1 (not expand)
    -- link 1.2
    --- link 1.2.1 (only)
    ---- link 1.2.1.1 (not expand)

我只能展开链接1.1,链接1.2 ...如何展开?

I can expand only link 1.1, link 1.2... how?

推荐答案

没有内置功能可用于扩展多个项目或在特定级别上扩展项目,因此没有其他方法可以遍历这些项目。调用 Expand 方法用于所有第二级项目。同样,在所有第一级项目上,否则将不会显示第二级项目。其 Recurse 参数应为 False ,以便不扩展可能的第三级或更深的级别。

There is no build-in functionality for expanding multiple items or items on a specific level, so there is no other way then traversing through the items. Call the Expand method on all second level items. As well on all first level items, otherwise the second level items will not be shown. Its Recurse parameter should be False in order not to expand a possibly third or deeper level.

有两种方法遍历TreeView的项:按项索引和按节点。通常,对TreeView项的操作最好由Node完成,因为 Items 属性的getter遍历 all Items 来查找具有特定内容的单个Item指数。但是, TTreeNodes 缓存上一次检索到的项目,因此通过将循环索引增加1,可以将危害最小化。

There are two ways to traverse through a TreeView's items: by Item index and by Node. Normally, operations on a TreeView's items are preferably done by Node, because the Items property's getter traverses through all Items to find a single Item with a specific index. However, TTreeNodes caches the last retrieved item, thus by incrementing the loop Index with 1, harm will be minimized.

然后简单的解决方案变为:

The easy solution then becomes:

procedure ExpandTreeNodes(Nodes: TTreeNodes; Level: Integer);
var
  I: Integer;
begin
  Nodes.BeginUpdate;
  try
    for I := 0 to Nodes.Count - 1 do
      if Nodes[I].Level < Level then
        Nodes[I].Expand(False);
  finally
    Nodes.EndUpdate;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ExpandTreeNodes(TreeView1.Items, 2);
end;

请注意, Items 属性的getter仍然是叫了两次。尽管有缓存机制,但我认为仍然应该避免:

Note that the Items property's getter is still called twice. Despite the caching mechanism, I think this still should be avoided:

procedure ExpandTreeNodes(Nodes: TTreeNodes; Level: Integer);
var
  I: Integer;
  Node: TTreeNode;
begin
  Nodes.BeginUpdate;
  try
    for I := 0 to Nodes.Count - 1 do
    begin
      Node := Nodes[I];
      if Node.Level < Level then
        Node.Expand(False);
    end;
  finally
    Nodes.EndUpdate;
  end;
end;

但是您最好使用:

procedure ExpandTreeNodes(Nodes: TTreeNodes; Level: Integer);
var
  Node: TTreeNode;
begin
  Nodes.BeginUpdate;
  try
    Node := Nodes.GetFirstNode;
    while Node <> nil do
    begin
      if Node.Level < Level then
        Node.Expand(False);
      Node := Node.GetNext;
    end;
  finally
    Nodes.EndUpdate;
  end;
end;

这仍然会遍历所有项目。好吧,一次。但是,如果您有一棵非常大和/或很深的树,并且希望获得最大的效率,并且不关心可读性,或者只想尝试使用TreeView的Nodes来娱乐,请使用:

This still traverses áll Items. Ok, just once. But if you have a really big and/or deep tree, and you want maximum efficiency, and you do not care about readability, or you just want to experiment with a TreeView's Nodes for fun, then use:

procedure ExpandTreeNodes(Nodes: TTreeNodes; Level: Integer);
var
  Node: TTreeNode;
  Next: TTreeNode;
begin
  if Level < 1 then
    Exit;
  Nodes.BeginUpdate;
  try
    Node := Nodes.GetFirstNode;
      while Node <> nil do
      begin
        Node.Expand(False);
        if (Node.Level < Level - 1) and Node.HasChildren then
          Node := Node.GetFirstChild
        else
        begin
          Next := Node.GetNextSibling;
          if Next <> nil then
            Node := Next
          else
            if Node.Level > 0 then
              Node := Node.Parent.GetNextSibling
            else
              Node := Node.GetNextSibling;
        end;
      end;
  finally
    Nodes.EndUpdate;
  end;
end;

这篇关于如何只扩展到树视图的第二层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆