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.