Jon Hallin
Jon Hallin

Reputation: 508

Unable to programmatically expand TreeNode that is assigned to a TreeView

I am working with C++ and .NET 1.1. I have an issue with programmatically expanding TreeNode objects once they are assigned to a TreeView. When running the following code in debug mode:

TreeView* myTreeView = new TreeView();
TreeNode* myTreeNode = new TreeNode();
myTreeNode->Expand();
myTreeView->Nodes->Add(myTreeNode);
myTreeNode->Expand();

I can see that the IsExpanded property of myTreeNode is set to true when doing the first Expand(), but when the node is added to myTreeView IsExpanded is set to false, and the second Expand() has no effect at all.

Can anyone explain this behavior? I'm thinking there is a setting for the TreeView or something similar, but I haven't been able to find anything like that, and from the example code MS provides this should work just fine, so I'm probably missing something pretty obvious...

Upvotes: 3

Views: 887

Answers (2)

Paul Williams
Paul Williams

Reputation: 17030

Have you tried listening for the TreeView.AfterCollapse event to see if someone else is collapsing the TreeNode after you add it to the TreeView?

Upvotes: 1

Phil Wright
Phil Wright

Reputation: 22936

I suggest adding a myTreeNode->Collapse() before calling the expand. It could be that the node thinks it is expanded when it is not and so calling expand will just be ignored because the node thinks it is already expanded anyway...

  TreeView* myTreeView = new TreeView();
  TreeNode* myTreeNode = new TreeNode();
  myTreeNode->Expand();
  myTreeView->Nodes->Add(myTreeNode);
  myTreeNode->Collapse();
  myTreeNode->Expand();

Upvotes: 1

Related Questions