473,385 Members | 1,940 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,385 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 5408
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...

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.