I'll assume you mean copy instead of move with Clone()...
I'll just grab a random worm from the can and start there...
The TreeNode.Clone method is documented as being "... not intended to be
used directly from your code". Yes, that's kind of silly because it's a
member of the ICloneable interface, which should be available to anyone who
wants to use it. But, I think what they mean here is you shouldn't be using
it as a virtual member (e.g. overriding it) ..which probably means it's being
used for something internally that could be affected by overriding it.
Next worm, I haven't seen anything specific about overriding
ICloneable.Clon e. To a certain extent, it would depend on the base class.
With TreeNode.Clone it actually creates an object of the instantiated type
(e.g. you can cast it to your type and it won't be null). In the case of
TreeNode, I think you can get by with something like:
// syntactically having ICloneable is meaningless when deriving
// from TreeNode, you have to override it anyway.
// TreeNode.Clone is documented as internal, so I've added
// ICloneable to cover the bases.
class Class1 : System.Windows. Forms.TreeNode, ICloneable
{
//...
public override object Clone ( )
{
Class1 result = base.Clone() as Class1;
Debug.Assert(re sult != null);
if(result != null)
{
result.MyProper ty = this.MyProperty ;
}
return result;
}
}
....ignoring any other worms.
--
http://www.peterRitchie.com/
Microsoft MVP, Visual Development - C#
"Steven" wrote:
Hi,
I have created my own node (class MyNode : TreeNode) for a TreeView.
To populate the treeview, i use something like MyNode newNode = new
MyNode("Bla bla bla","0","1") for example.
But, to move the nodes in the treeview, i use the clone() method... :
MyNode theCopy = (MyNode)theTree View.SelectedNo de.Clone();
int leIndex = theTreeView.Sel ectedNode.Index ;
MyNode theParent = (MyNode)theTree View.SelectedNo de.Parent;
theTreeView.Nod es.Remove(theTr eeView.Selected Node);
if (theParent == null) theTreeView.Nod es.Insert(leInd ex - 1, theCopy );
else theParent .Nodes.Insert(l eIndex - 1, theCopy );
theTreeView.Sel ectedNode = theCopie;
.... and then i lose all my MyNode specific parameters.
I guess that it's because i use the Clone method of the TreeNode part of my
MyNode class.
I think i should override the Clone method in MyNode class, but i don't know
how to do this.
If you have any suggestion... Thanks !