A question on TreeViews

D

Dom

I'm driving myself nuts with this TreeView. I don't want to get into
the error, but I want to give you what I think is the cause of the
error, and then see if anyone more knowledgable can confirm this.

In the statement ...

foreach (TreeNode n in MyTree.Nodes) { NewTree.Nodes.Add ((TreeNode)
n.Clone()); }

.... I think I am just cloning the one node, and not it's children
nodes, or their children nodes, etc.

Now that I spell it out, that really seems right. Can anyone tell me
what I should be doing? Should I just make it all recursive?

Dom
 
R

Random

... I think I am just cloning the one node, and not it's children
nodes, or their children nodes, etc.

Nope. The documentation for TreeView.Clone says:
"Copies the tree node and the entire subtree rooted at this tree
node."
Now that I spell it out, that really seems right.  Can anyone tell me
what I should be doing?  Should I just make it all recursive?

What exactly is your problem?
 
P

Peter Duniho

Dom said:
I'm driving myself nuts with this TreeView. I don't want to get into
the error, but I want to give you what I think is the cause of the
error, and then see if anyone more knowledgable can confirm this.

In the statement ...

foreach (TreeNode n in MyTree.Nodes) { NewTree.Nodes.Add ((TreeNode)
n.Clone()); }

.... I think I am just cloning the one node, and not it's children
nodes, or their children nodes, etc.

Now that I spell it out, that really seems right. Can anyone tell me
what I should be doing? Should I just make it all recursive?

You probably shouldn't be using the Clone() method at all. But yes, the
Clone() method does what's known as a "shallow copy". That is, only the
object specifically being used to call Clone() is copied. That copy
winds up referencing all the same other objects that the original
referenced.

If you want the entire sub-tree of nodes to be cloned, you need to
recursively clone them manually. That said, it's more likely that the
right way to do this particular task is to not use the Clone() method at
all, and instead recursively re-create new TreeNode objects using the
pertinent data from the originals (e.g. the Value property). So much of
the new node's data will have to be different, I see little value in
actually copying it literally before you fix up everything else.

Pete
 
D

Dom

You probably shouldn't be using the Clone() method at all.  But yes, the
Clone() method does what's known as a "shallow copy".  That is, only the
object specifically being used to call Clone() is copied.  That copy
winds up referencing all the same other objects that the original
referenced.

If you want the entire sub-tree of nodes to be cloned, you need to
recursively clone them manually.  That said, it's more likely that the
right way to do this particular task is to not use the Clone() method at
all, and instead recursively re-create new TreeNode objects using the
pertinent data from the originals (e.g. the Value property).  So much of
the new node's data will have to be different, I see little value in
actually copying it literally before you fix up everything else.

Pete

Thanks, I think I'll do the recursion without the Clone method. For
the record, the problem was that when I made changes that effected the
TAG property of the tree nodes in the clone, the TAG of the original
was changed also. Perhaps it is only the tag that needs to be created
new?
 
P

Peter Duniho

Dom said:
Thanks, I think I'll do the recursion without the Clone method. For
the record, the problem was that when I made changes that effected the
TAG property of the tree nodes in the clone, the TAG of the original
was changed also. Perhaps it is only the tag that needs to be created
new?

Probably. I overlooked that the TreeNode.Clone() method is specifically
implemented as a deep copy, but yes...even that deep copy can't clone
objects that themselves aren't cloneable. In fact, I wouldn't be
surprised if the _only_ thing about TreeNode.Clone() that's "deep" are
the other TreeNode children.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top