Let's say I've got tree with 3 categories, each with 3 child nodes. I want to delete root node, when all child nodes gets deleted. I tried something like this:
TreeNode current = treeView1.SelectedNode;
TreeNode parent = treeView1.SelectedNode.Parent;
if (parent.Nodes.Count == 0)
{
parent.Nodes.Remove(current);
}
And I placed it in Form1_Load. Unfortunatelly, when all child nodes are gone nothing happens. Is this code correct? Or maybe I misplaced it and I should place it somewhere else?
edit: My tree looks like this:
Morning
- brush teeth
- drink coffee
Afternoon
- dinner
- TV
Night
- Sleep
So if I decide to delete "Sleep", I want also delete "Night". But If I decide to delete "TV", I want to keep "Dinner" and "Afternoon".
NullReferenceException
thrown,parent.Nodes.Count
will always be at least 1. – Simon Belanger