Search code examples
c#.netwinformstreeviewtreeviewitem

Treeview remove property problem


i added a SampleNode to the treeview.

if i remove any node from SampleNode,

like,

TreeNode[] nodes = this.SampleNode.Nodes.Find(node.Text, true);
      if (nodes.Length > 0)
      {
        int j = nodes[0].Index;
        if (nodes.Length > 0)
          this.SampleNode.Nodes[j].Remove();
      }

it is deleted in treeview but not in SampleNode. Why does this happens?

what is the solution?


Solution

  • Code to add some nodes.

     TreeNode root = new TreeNode("Root");
    
     root.Nodes.Add("1", "Sampl1");
     root.Nodes.Add("2", "Sampl2");
     root.Nodes.Add("3", "Sampl3");
     treeView1.Nodes.Add(root);
    

    Code to search and delete a node,

    TreeNode []nodes= treeView1.Nodes.Find("1", true);
    
     if (nodes.Length != 0)
      {
        //nodes[0].Remove();
        //or
        treeView1.Nodes.Remove(nodes[0]);
      }