Cloning a class derived from TreeNode

K

kurotsuke

Hi,

I need to clone a class (called NodeAbstract) that I derived from
TreeNode. I need to clone it to support drag and drop on the treeview.

I tried to use the MemberWiseClone (in my own Clone() method) method
but with no success. The cloned object seems to be correctly created
but I cannot add it to the treeview (I get no error message but simple
the new node isn't added).

If I use the standard TreeNode Clone method it works fine but of
course none of my specific fields are copied.

I tried to copy all the members directly from the original node to the
cloned object but how could I access the private members?

Thanks.
 
K

kurotsuke

Could you show a small example of this in code?

This is the code I'm using in the DragDrop

newNode = (NodeAbstract) senderNode.Clone();
senderNode.Remove();
treevew1.Nodes.Add(newNode);

Actually, I'm working with NodeAbstract: TreeNode and
NodeFile:NodeAbstract

The code I wrote in the NodeAbstract.Clone() is

public override object Clone()
{
NodeAbstract cloned = (NodeAbstract) MemberwiseClone();
return cloned;
}

The object seems to be created correctly but I cannot add it to the
treeview. If I use the standard TreeNode clone all works correctly
execept for the custom properties.

In case I want to do the copy manually, how can I access all the
private properties of the cloned object?
Thanks.
 
W

William Stacey [MVP]

Has something to do with the private TreeView field I think. The value
needs to get "Nulled" I think *before the Clone, otherwise the Add() sees
that it has a TreeView ref already and figures it is not a new TreeNode. So
Remove the node first (which internally nulls that field), then Clone() it,
then Add the clone like:

// Drag/Drop Move Method.
MyNode myNode = (MyNode)this.treeView1.TopNode;
myNode.Remove(); // Remove the Node which clears required TreeNode
members.
MyNode copy = (MyNode)myNode.Clone();
treeView1.Nodes.Add(copy);

public override object Clone()
{
MyNode clone = (MyNode)this.MemberwiseClone();
return clone;
}

This worked for me. At first ran into same issue you had with the clone.
Not sure about any issues that may be involved with MemeberwiseClone. Also
this does not clone all the treeNodes down the tree from node down. So if
you need this, you need to do that yourself.
Another solution is to just "new" up a new MyNode and "clone" the required
members yourself (including any hierarchy.)

Another solution would be to Serialize graph with xml and Derialize the same
way. That way Cut and Paste can work nice too using the Clipboard or sent
via email, saved, etc. I did clipboard method this way myself and am happy
with it so far. Just a thought.

Anyway hope that helped. Cheers!
 
K

kurotsuke

Thanks a lot. You were right. I moved the Remove statement before the
Clone and now it works perfectly,
Your are really an asset for this newsgroup.
Thanks again.
 
K

kurotsuke

Has something to do with the private TreeView field I think. The value
needs to get "Nulled" I think *before the Clone, otherwise the Add() sees
that it has a TreeView ref already and figures it is not a new TreeNode. So
Remove the node first (which internally nulls that field), then Clone() it,
then Add the clone like:

// Drag/Drop Move Method.
MyNode myNode = (MyNode)this.treeView1.TopNode;
myNode.Remove(); // Remove the Node which clears required TreeNode
members.
MyNode copy = (MyNode)myNode.Clone();
treeView1.Nodes.Add(copy);

public override object Clone()
{
MyNode clone = (MyNode)this.MemberwiseClone();
return clone;
}

This worked for me. At first ran into same issue you had with the clone.
Not sure about any issues that may be involved with MemeberwiseClone. Also
this does not clone all the treeNodes down the tree from node down. So if
you need this, you need to do that yourself.
Another solution is to just "new" up a new MyNode and "clone" the required
members yourself (including any hierarchy.)

Another solution would be to Serialize graph with xml and Derialize the same
way. That way Cut and Paste can work nice too using the Clipboard or sent
via email, saved, etc. I did clipboard method this way myself and am happy
with it so far. Just a thought.

Anyway hope that helped. Cheers!

Hi,
I've just one more problem. When I drag and drop a node that contains
other nodes (child nodes) these are deleted. How can I avoid that?
Thanks again.
 
W

William Stacey [MVP]

As your just moving it, I think you can just skip the clone stuff and:
1) Get ref to node.
2) Remove the node.
4) Add the node to dest node. This will include all the children.

Pretty much same as other code without the clone step. hth.
 
K

kurotsuke

As your just moving it, I think you can just skip the clone stuff and:
1) Get ref to node.
2) Remove the node.
4) Add the node to dest node. This will include all the children.

Pretty much same as other code without the clone step. hth.

You are a genius. Easy and beatiful solution. My congratulations. Your
help has really been invaluable.
 
R

Richard Urwin

I'm really new to C# - please can you tell me how to get a reference
to a node? I need to do exactly the same thing...

cheers,
rich
 

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