12

I want to expand tree on main form when application starts. How i can do it? I cant find corresponding property. C++ builder 2009.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Funtime
  • 2,466
  • 4
  • 20
  • 21

3 Answers3

33

You simply need to call FullExpand() on the tree view.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 1
    How do i jump to the first item when expanded not at the end. The scrollbar is at the maximum bottom. – Hidden Jan 22 '15 at 10:31
  • 3
    @TheAllSeeingEye Perhaps using `TopItem` and/or `Select`. – David Heffernan Jan 22 '15 at 12:18
  • @Hidden: I can't figure out the issue with scrollbar. I was trying these two things but I have still this issue. `tvTreeView.Selected := tvTreeView.TopItem`or `tvTreeView.Select(tvTreeView.Item[0])`- it doesn't work for me. How did you fix that? – astack Dec 06 '16 at 18:47
  • 1
    I figured it out. The issue was with `TopItem` property and I had to set it manually. Maybe it will be helpful for somebody. `tvTreeView.FullExpand;` `tvTreeView.TopItem := tvTreeView.Items.GetFirstNode;` – astack Dec 06 '16 at 21:22
  • That's what I suggested in my comment I think – David Heffernan Dec 06 '16 at 21:23
1

When adding treenode make its expanded property to true

you will find a property for the treeNode Object, set it yo true before add to list of nodes.

and also you can find a method for the treeView called ExpandAll

My Regards


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's 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;

and see the reference http://www.delphipages.com/forum/showthread.php?t=159148

My Regards

Andriy M
  • 76,112
  • 17
  • 94
  • 154
Mhmd
  • 4,989
  • 3
  • 22
  • 29
  • TTreeView don't have ExpandAll method. All objects to TreeViw was added in desing time – Funtime Apr 10 '11 at 15:30
  • 1
    On StackOverflow you do not need to use /pre or whatever to format code. All you need to do is indent it by four spaces. It would also help if you didn't use tabs but spaces to indent your statements. For inline code you can surround the code with "back quote" characters. The back quote (\`) is usually found together with the tilde (~). The back quote can also be used in comments: `like so`. – Marjan Venema Apr 10 '11 at 19:04
0

There a number of Ways of doing this. The easiest would be

TreeView1.FullExpand;

as in the 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 on a TTreeNode is useful if you want to fully expand from a particular node that is not the root node.

Alister
  • 6,527
  • 4
  • 46
  • 70