views:

239

answers:

3

Hi, I have a strange problem. Let's look at that code:

TreeNode tn = TreeView1.FindNode("2009/08/12 (1)");     //OK, the Node is found

Now, I need to delete that node:

(IT DOESN'T WORK !)

(e.g. (I know that I don't need to use TreeView1.FindNode() method, but i = -1))

            TreeNode tn1 = TreeView1.FindNode(tn.ValuePath);
            int i = TreeView1.Nodes.IndexOf(tn1);

or

            TreeView1.Nodes.Remove(tn);

The problem is, the codes above doesn't work, I mean, the node isn't removed, why ? The TreeView looks like that:

alt text

A: 

Are you sure that you've selected the node properly? If TreeView1.Nodes.IndexOf(tn1) is returning -1, this indicates the node can't be found.

pmarflee
A: 

and that's the point, once, the node is found (tn.ValuePath is "2009/08/12 (1)") and one line below the index of that node is -1

Tony
A: 

It seems that the TreeView control in .net only allows to remove First Level Nodes, so if the node you are trying to delete is not this kind of node, you need to delete it trough its parent, using something like this:

Dim Padre As TreeNode = TreeView1.SelectedNode.Parent If (Padre Is Nothing) Then TreeView1.Nodes.Remove(TreeView1.SelectedNode) Else Padre.ChildNodes.Remove(TreeView1.SelectedNode) End If

Hope it helps!

Tom

tomasofen