1

I am trying to check whether a tree node is expanded or not, but unfortunately, it appears that the TreeNode. Expanded field is always null until the user either expands or collapses a node. However, as the Tree View starts out with only one level expanded (there's too much information for all levels to be expanded at the beginning), this is not accurate and cannot be solved by simply checking for null or expanded (which is what I had assumed would work at first). Any ideas on what to do?

3
  • Check if it is null or open? Commented Aug 11, 2017 at 13:28
  • Have you checked to see if it is null when collapsed or expanded? If the user hasn't interacted with it, why care? What are your goals here? Commented Aug 11, 2017 at 13:28
  • @Will The tree starts out not fully expanded, but the nodes don't seem to register that. Commented Aug 11, 2017 at 13:32

1 Answer 1

1

I solved the problem by programmatically collapsing all the nodes in the tree, then re-expanding the ones I wanted expanded using the method below:

private void ExpandTree(int Depth)
{
    tr.CollapseAll();

    foreach (TreeNode tn in tr.AllNodes().Where(tn => tn.Depth < Depth))
    {
        tn.Expand();
    }
}

AllNodes() is an extension method that gets all nodes in the tree view, breadth-first.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.