469,645 Members | 1,941 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,645 developers. It's quick & easy.

Cloning a class derived from TreeNode

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.
Nov 16 '05 #1
8 5195
Could you show a small example of this in code?

--
William Stacey, MVP

"kurotsuke" <ku*******@yahoo.it> wrote in message
news:c4********************************@4ax.com...
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.


Nov 16 '05 #2
On Tue, 3 Aug 2004 17:56:06 -0400, "William Stacey [MVP]"
<st***********@mvps.org> wrote:
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.
Nov 16 '05 #3
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!

--
William Stacey, MVP

"kurotsuke" <ku*******@yahoo.it> wrote in message
news:m6********************************@4ax.com...
On Tue, 3 Aug 2004 17:56:06 -0400, "William Stacey [MVP]"
<st***********@mvps.org> wrote:
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.


Nov 16 '05 #4
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.
Nov 16 '05 #5
On Wed, 4 Aug 2004 12:18:09 -0400, "William Stacey [MVP]"
<st***********@mvps.org> wrote:
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.
Nov 16 '05 #6
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.

--
William Stacey, MVP

"kurotsuke" <ku*******@yahoo.it> wrote in message
news:qc********************************@4ax.com...
On Wed, 4 Aug 2004 12:18:09 -0400, "William Stacey [MVP]"
<st***********@mvps.org> wrote:
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. SoRemove 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. Alsothis does not clone all the treeNodes down the tree from node down. So ifyou need this, you need to do that yourself.
Another solution is to just "new" up a new MyNode and "clone" the requiredmembers yourself (including any hierarchy.)

Another solution would be to Serialize graph with xml and Derialize the sameway. That way Cut and Paste can work nice too using the Clipboard or sentvia email, saved, etc. I did clipboard method this way myself and am happywith 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.


Nov 16 '05 #7
On Thu, 5 Aug 2004 12:04:47 -0400, "William Stacey [MVP]"
<st***********@mvps.org> wrote:
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.
Nov 16 '05 #8
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

kurotsuke <ku*******@yahoo.it> wrote in message news:<sk********************************@4ax.com>. ..
On Thu, 5 Aug 2004 12:04:47 -0400, "William Stacey [MVP]"
<st***********@mvps.org> wrote:
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.

Nov 16 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Baris | last post: by
reply views Thread by meh | last post: by
10 posts views Thread by Joe | last post: by
3 posts views Thread by AVL | last post: by
10 posts views Thread by pazabo | last post: by
reply views Thread by Chris | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.