Off the top of my head:
void DeleteNode (TreeView parent_tree, TreeNode selected_node)
{
if (selected_node != null)
{
for (int x = 0; x < selected_node.Nodes.Count; x++)
DeleteNode (selected_node.Nodes[x]);
// logic for removing node from parent_tree.
}
}
This is a recursive function that loops through and deletes all the child nodes of a particular node before deleting that node. Note that each time the function gets called, it first loops into the child nodes of the one passed in the parameter, until one is reached that has no children. THEN the logic for removing the node from the tree is executed.
The EXIT CONDITION for this recursive logic is to reach a node that has no children. This is reasonable, so long as we no that the tree view doesn't have an infinite child structure (which it shouldn't). Alternatively, a null value in the selected_node parameter will stop the function.
Be careful with recursive functions! Incorrectly used, they can create bad stuff!
Brandon
|