473,390 Members | 1,753 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,390 software developers and data experts.

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 5410
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Baris | last post by:
Given the C# code below, can anyone think of a better class design? What really gets my goat is that the code within derived classes D1 and D2 is identical and in my mind should be refactored into...
0
by: meh | last post by:
Greetings all; Got some questions about cloning a treenode.... In this example: private void button4_Click(object sender, System.EventArgs e) { TreeNode lastNode = treeView1.Nodes....
0
by: Richard | last post by:
I need a deep clone of my derived TreeNode class. Can anyone help me writting a clone() override that will do this?
0
by: Richard | last post by:
I need a deep clone of my derived TreeNode class. Can anyone help me writting a clone() override that will do this? I've seen examples like this,...
11
by: K.K. | last post by:
Suppose I have a class called Vehicle with many fields. Now I make a new class derived from Vehicle called Car. I'd like to make a method to copy the data from a Vehicle instance to a Car instance....
10
by: Joe | last post by:
My question is more an OOD question. I know *how* to implement both abstract classes and interfaces. Here's my question - under what circumstacnes does one use an abstract class and under what...
3
by: AVL | last post by:
Hi, I've a query in cloning. How cloning is different from creating a new instance of an object.? I suppose cloning also creates a new object and copies the exisitng object's data. Where and when...
10
by: pazabo | last post by:
Hi, I'm trying to somehow implement polymorphic object cloning (just as it is in Java), but when I write: class Object { public: virtual Object * clone() const = 0; // ... }
0
by: Chris | last post by:
Hi All. I'm cloning a treenode, like so: TreeNode trNewTemp = new TreeNode(); trNewTemp = ((((ICloneable)trTempNode).Clone()) as TreeNode); Now the cloning works- trNewTemp has the same...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.