سؤال

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?

هل كانت مفيدة؟

المحلول

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]);
  }

نصائح أخرى

The problem is might be that you are removing nodes by the index in the nodes array - not by their position in the tree.

Try replacing:

this.SampleNode.Nodes[j].Remove();

With:

this.SampleNode.Nodes.Remove(nodes[0]); 
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top