Category / Section
How to delete the selected node in the WinForms TreeViewAdv control?
1 min read
Delete the selected node
C#
this.treeViewAdv1.Nodes.Remove(this.treeViewAdv1.SelectedNode);
The above code will only delete the selected node if it is a root node. This is because TreeViewAdv.Nodes collection does not contain all the nodes of the tree, just thelevel 0 nodes. If the node is in a level greater than 0, it is not deleted as it is not contained in the primary Nodes collection. So, you instead should call:
C#
TreeViewAdv.SelectedNode.Parent.Nodes.Remove(TreeViewAdv.SelectedNode);
VB
TreeViewAdv.SelectedNode.Parent.Nodes.Remove(TreeViewAdv.SelectedNode)