How to automatically expand all TTreeView nodes?

I want to expand the tree in the main form when the application starts. How can i do this? I can not find the corresponding property. C ++ builder 2009.

+5
source share
3 answers

You just need to call FullExpand()in the tree view.

+25
source

When treenode is added, its extended property is true

you will find the property for the treeNode object, set it to yo true before adding it to the list of nodes.

and also you can find a method for treeView called ExpandAll

Congratulations


try this code

//this will expand all nodes of Level and their parents
procedure ExpandTree(Tree: TTreeView; Level: integer);

  procedure ExpandParents(Node: TTreeNode);
  var
    aNode : TTreeNode;
  begin
    aNode := Node.Parent;
    while aNode <> nil do begin
      if not aNode.Expanded then
        aNode.Expand(false);
      aNode := aNode.Parent;
    end;
  end;

var
  aNode : TTreeNode;
begin
  if Tree.Items.Count > 0 then begin
    aNode := Tree.Items[0];

    while aNode <> nil do begin
      if aNode.Level = Level then begin
        aNode.Expand(false);
        ExpandParents(aNode);
      end;
      aNode := aNode.GetNext;
    end;
  end;
end;

//this will expand the Node and it parents
procedure ExpandNode(Node: TTreeNode);
var
  aNode : TTreeNode;
begin
  Node.Expand(false);

  aNode := Node.Parent;
  while aNode <> nil do begin
    if not aNode.Expanded then
      aNode.Expand(false);
    aNode := aNode.Parent;
  end;
end;

. http://www.delphipages.com/forum/showthread.php?t=159148

+1

There are several ways to do this. It would be easiest

TreeView1.FullExpand;

as in accepted answer - Alternatively

if TreeView1.items.GetFirstNode <> nil then
  TreeView1.items.GetFirstNode.Expand(True);

or

if TreeView1.items[0] <> nil then
  TreeView1.items[0].Expand(True);

The Expand method in TTreeNode is useful if you want to fully extend it from a specific node, which is not the root node.

0
source

All Articles