I have a TreeView that is empty at application start. Nodes are dynamically added, and for each node I put its tag into a dictionary.
Now, if I have many long branches of nodes and I delete the root node and its tag from the dictionary, then of course the child nodes tags will stay in the dictionary.
So I tried this:
foreach(TreeNode tn in treeView.SelectedNode.Nodes)
{
dictionary.Remove((string)tn.Tag); // remove all respective keys
}
dictionary.Remove((string)treeView.SelectedNode.Tag); // remove the selected node's key
treeView.Nodes.Remove(treeView.SelectedNode); // remove the selected node itself
But this only deletes the selected node and the first child node. Is there a way to do this recursively, from the top of a tree to the root, so that each key gets savely removed?