473,624 Members | 2,565 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Treeview and Clone() method.

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 !
FB.
Jul 3 '06 #1
2 11457
>
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.Sel ectedNode;
int leIndex = theTreeView.Sel ectedNode.Index ;

MyNode theParent = (MyNode)theTree View.SelectedNo de.Parent;

theTreeView.Nod es.Remove(tNode );

if (theParent == null) {
theTreeView.Nod es.Insert(leInd ex - 1, tNode );
}
else {
theParent .Nodes.Insert(l eIndex - 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 TreeNodeCollect ion (say alphabetical, or
based on the node type) and pass into it the node to be moved and the
TreeNodeCollect ion 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.

Jul 3 '06 #2
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 !
Jul 3 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2659
by: Jason Evans | last post by:
Hi All, I am writing my own implementation of queue via a linked list, note not a LinkedList, and was running into trouble with the clone method. I was wondering if anyone could point out some not so obvious errors I have made.. Cheers
4
6259
by: Steve Teeples | last post by:
I have TreeNodes in a TreeView, each contains unique data in its Tag section. I am trying to 'clone' a TreeNode and then modify the tag data of the cloned TreeNode. What I am seeing is that by modifying the Tag data of the cloned TreeNode the original TreeNode's Tag data is also being modified with the identical change. Does the clone method not create a completely new data reference of the original TreeNode? If not, how can I get a...
0
1534
by: Andre Viens | last post by:
Hello, I have a TreeView I'm using to navigate through various options shown in a ListView. I want to use the SelectedNode.Tag property to hold the items in my ListView. Everything is working great, except... Private Sub tvBookmarks_BeforeSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles tvBookmarks.BeforeSelect
3
1410
by: Kelvin Leung | last post by:
Hi I use Drag and Drop between 2 TreeView Control under VB.Net But I found that it cannot work when I add sub-class for each node Is it drag and drop method cannot work when the node with sub-class ? If no, any related information/reference ? Thanks a lot
3
3785
by: Gary Dunne | last post by:
I'm writing an app that requires drag and drop operation between a ListView and a TreeView control. (The source is the ListView). During the drag drop operation I want to be able to detect the target node in the treeview and auto expand it if applicable... but after a fair bit of head scratching I can't find any easy way to accomplish this. I think what i really need is the equivalent of the HitTest method from the COM version of the...
16
2503
by: Hamed | last post by:
Hello I am developing a utility to be reused in other programs. It I have an object of type Control (a TextBox, ComboBox, etc.) that other programmers use it in applications. they may set some of properties or assign event handlers. I need to be able to clone the manipulated control at runtime. I could use the Clone method of some objects (like Font, Style, String,
14
8611
by: Hamed | last post by:
Hello It seems that I should implement ICloneable to implement my own clone object. the critical point for me is to make a control object based on another control object that all of its event handlers are set like the old one. Is there a way to do this job? For example, is there a way to use EventInfo object to get all event handlers of the old control in runtime and set my new cloned control events to the event handlers of the old...
2
1545
by: casManG | last post by:
I am working on a small project that uses the treeview control in .net 2003. I have a tree view that I am sending to a sub in order to iterate through the nodes. Public Sub test (ByVal inTreeView as Tree View) But, the thing I want to do with the inTreeView requires me to expand all the nodes before I iterate. The problem is that when the sub is complete, the original tree view on my form ends up with all of the nodes expanded I had...
7
1472
by: John Rogers | last post by:
I had given up on this for a while, but I am attempting it again. This is what i am using to store my treeview layout into a db, I am only using a few fields to make it easier to understand while I am attempting to restore it back into the tree. Text "Parent0", Level 0 Text "Child0", Level 1 Text "SubChild0", Level 2
0
8249
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8179
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8633
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8493
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6112
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1797
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1493
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.