Treeview and Clone() method.

  • Thread starter Thread starter Steven
  • Start date Start date
S

Steven

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)theTreeView.SelectedNode.Clone();

int leIndex = theTreeView.SelectedNode.Index;
MyNode theParent = (MyNode)theTreeView.SelectedNode.Parent;
theTreeView.Nodes.Remove(theTreeView.SelectedNode);
if (theParent == null) theTreeView.Nodes.Insert(leIndex - 1, theCopy );
else theParent .Nodes.Insert(leIndex - 1, theCopy );
theTreeView.SelectedNode = 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 !
FB.
 
But, to move the nodes in the treeview, i use the clone() method... :

Not the asked question but may solve your problem.
Why are you creating a copy?
Why not move the selectedNode?


TreeNode tNode = theTreeView.SelectedNode;
int leIndex = theTreeView.SelectedNode.Index;

MyNode theParent = (MyNode)theTreeView.SelectedNode.Parent;

theTreeView.Nodes.Remove(tNode);

if (theParent == null) {
theTreeView.Nodes.Insert(leIndex - 1, tNode );
}
else {
theParent .Nodes.Insert(leIndex - 1, tNode );
}


Other points
-----------------
It is not neccessary to cast the parent to MyNode. All you are using
from it is the Nodes collection which is part of the TreeNode
interface.

I am dubious on using the index to position the node in the new
collection. What happens if there are fewer nodes in the target? You
may get an out of range exception.

You might try having another routine be reponsible for positioning a
newly added node within a TreeNodeCollection (say alphabetical, or
based on the node type) and pass into it the node to be moved and the
TreeNodeCollection to which it is to be moved.
I think i should override the Clone method in MyNode class, but i don't know
how to do this.

public override object Clone()
{
MyNode tNode = (MyNode) base.Clone ();

// set the properties for the MyNode
// e.g.

tNode._flavor = _flavor;

return tNode;

}


hth,
Alan.
 
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.Clone. 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(result != null);
if(result != null)
{
result.MyProperty = this.MyProperty;
}
return result;
}
}

....ignoring any other worms.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top