Hi All,

I need help with adding and deleting nodes(parent nodes) and subnodes(child nodes) in a TreeView. I have a form with a TreeView control that is pre-populated with two parent nodes (named "Finances" and "Personal")both containing two child nodes each when the form is loaded. I would like to add two buttons to this form that would allow me to add new nodes and delete existing nodes from the treeview at run time. Any help with the code for the event handlers for these two buttons is much appreciated.
Below is a snippet of code i have written thus far.

private void frmTreeViewDemo_Load(object sender, EventArgs e)
        {
            treeView1.Nodes.Clear();
            // add a root node to the treeview
            treeView1.Nodes.Add("My Wallet");
           
            // create a new treeNode and add sub or child nodes to it
            TreeNode node = new TreeNode("Finances");
            node.Nodes.Add("Bank Account");
            node.Nodes.Add("Credit Card");
            // add another node with child nodes
            TreeNode node1 = new TreeNode("Personal");
            node1.Nodes.Add("ID Card");
            node1.Nodes.Add("Passport");
           
            treeView1.Nodes[0].Nodes.Add(node);
            treeView1.Nodes[0].Nodes.Add(node1);
            treeView1.Nodes[0].Expand();
        }

Add two buttons btnAdd, btnDelete.. create event handler (onClick) for both

private void btnAdd_Click(object sender, EventArgs e)
        {
            TreeNode selectedNode = treeView1.SelectedNode;
            selectedNode.Nodes.Add("New node"); //prompt it as textbox or by any way you see..
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            TreeNode selectedNode = treeView1.SelectedNode;
            if (selectedNode.Parent == null)
                MessageBox.Show("Can't remove root node");
            else
                selectedNode.Remove();
        }

Add two buttons btnAdd, btnDelete.. create event handler (onClick) for both

private void btnAdd_Click(object sender, EventArgs e)
        {
            TreeNode selectedNode = treeView1.SelectedNode;
            selectedNode.Nodes.Add("New node"); //prompt it as textbox or by any way you see..
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            TreeNode selectedNode = treeView1.SelectedNode;
            if (selectedNode.Parent == null)
                MessageBox.Show("Can't remove root node");
            else
                selectedNode.Remove();
        }

Thanks Ramy. What if i want to add a new child node to a particular parent node? How do i get a reference to that parent node so that the child node is inserted in the correct position?

Select it then press the button... if you don't like to do it this way.. you may then replace selectedNode in my code with specific node.

Select it then press the button... if you don't like to do it this way.. you may then replace selectedNode in my code with specific node.

Ok. I tried your original solution and it works. However i have a problem when it comes to prompting for the new node via a textbox. I've added a second form with a text box and a "Save" button. When i click this "Save' button, how do i get the node entered in the text box to appear in the tree view?
thanks for any suggestions

dear it's another question :) please mark it as solved to help people find relevant threads.
Start new thread with another question.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.