Search code examples
c#treeviewcrashparent

.NET TreeView causes application to crash when trying to check Parent node


I have a TreeView with check boxes, and when a user checks a child node, I want to go up the tree and check each parent. However, for some reason my application blows up whenever I touch the parent node. For the nodes in my tree, I've extended TreeNode to create my own objects with some data that I need to store in them, but I still reference them as TreeNodes when checking/unchecking. My code looks like this:

//checkBox checked event handler
    if (node.Parent != null)
    {
        checkAllParents(node.Parent);
    }
//

private void checkAllParents(TreeNode node)
{
    node.Checked = true;
    if (node.Parent != null)
    {
       checkAllParents(node.Parent);
    }
}

Solution

  • Ok, figured it out. It wasn't a circular reference but it was most certainly circular in nature. This was a big dumb mistake on my part..in the event handler I was using recursion to check DOWN the tree as well...I implemented this a while ago and didn't really think about it, so when I added another piece of recursion to check up the tree I ended up with an infinite loop between the recursion function and the event handler (which was getting called every time the recursive function checked one of the nodes..the After_checked event). Bah.