473,402 Members | 2,053 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,402 software developers and data experts.

Adding the same TreeNode to multiple TreeNodes

When I attempt to run the following code:

protected void NavigationTreeView_TreeNodePopulate(object sender,
TreeNodeEventArgs e) {
TreeNode tn1 = new TreeNode("node1","node1");
TreeNode tn2 = new TreeNode("node2", "node2");
TreeNode newNode = new TreeNode("add me to both nodes", "add me to
both nodes");
e.Node.ChildNodes.Add(tn1);
e.Node.ChildNodes.Add(tn2);
tn1.ChildNodes.Add(newNode);
tn2.ChildNodes.Add(newNode);
}

It does not behave as expected. Instead of adding a copy of newNode to each
of the other two nodes, it adds to tn1 and then when it adds it to tn2 it
removes it from tn1, thus in the end only tn2 has the newNode and tn1 has no
children. This surprises me. How can I add the same node to two separate
locations in a treeview?
Apr 25 '06 #1
4 3570
try this

tn1.ChildNodes.Add(newNode.Clone());
tn2.ChildNodes.Add(newNode.Clone());

"Turban" <Tu****@discussions.microsoft.com> wrote in message
news:8E**********************************@microsof t.com...
When I attempt to run the following code:

protected void NavigationTreeView_TreeNodePopulate(object sender,
TreeNodeEventArgs e) {
TreeNode tn1 = new TreeNode("node1","node1");
TreeNode tn2 = new TreeNode("node2", "node2");
TreeNode newNode = new TreeNode("add me to both nodes", "add me to
both nodes");
e.Node.ChildNodes.Add(tn1);
e.Node.ChildNodes.Add(tn2);
tn1.ChildNodes.Add(newNode);
tn2.ChildNodes.Add(newNode);
}

It does not behave as expected. Instead of adding a copy of newNode to
each
of the other two nodes, it adds to tn1 and then when it adds it to tn2 it
removes it from tn1, thus in the end only tn2 has the newNode and tn1 has
no
children. This surprises me. How can I add the same node to two separate
locations in a treeview?

Apr 25 '06 #2
Unfortunately, it seems that the Clone() method on TreeNode is inaccessible.

"AMDRIT" wrote:
try this

tn1.ChildNodes.Add(newNode.Clone());
tn2.ChildNodes.Add(newNode.Clone());

"Turban" <Tu****@discussions.microsoft.com> wrote in message
news:8E**********************************@microsof t.com...
When I attempt to run the following code:

protected void NavigationTreeView_TreeNodePopulate(object sender,
TreeNodeEventArgs e) {
TreeNode tn1 = new TreeNode("node1","node1");
TreeNode tn2 = new TreeNode("node2", "node2");
TreeNode newNode = new TreeNode("add me to both nodes", "add me to
both nodes");
e.Node.ChildNodes.Add(tn1);
e.Node.ChildNodes.Add(tn2);
tn1.ChildNodes.Add(newNode);
tn2.ChildNodes.Add(newNode);
}

It does not behave as expected. Instead of adding a copy of newNode to
each
of the other two nodes, it adds to tn1 and then when it adds it to tn2 it
removes it from tn1, thus in the end only tn2 has the newNode and tn1 has
no
children. This surprises me. How can I add the same node to two separate
locations in a treeview?


Apr 25 '06 #3
Sorry...I should have noted that this is the web control:
System.Web.UI.WebControls.TreeNode

"AMDRIT" wrote:
try this

tn1.ChildNodes.Add(newNode.Clone());
tn2.ChildNodes.Add(newNode.Clone());

"Turban" <Tu****@discussions.microsoft.com> wrote in message
news:8E**********************************@microsof t.com...
When I attempt to run the following code:

protected void NavigationTreeView_TreeNodePopulate(object sender,
TreeNodeEventArgs e) {
TreeNode tn1 = new TreeNode("node1","node1");
TreeNode tn2 = new TreeNode("node2", "node2");
TreeNode newNode = new TreeNode("add me to both nodes", "add me to
both nodes");
e.Node.ChildNodes.Add(tn1);
e.Node.ChildNodes.Add(tn2);
tn1.ChildNodes.Add(newNode);
tn2.ChildNodes.Add(newNode);
}

It does not behave as expected. Instead of adding a copy of newNode to
each
of the other two nodes, it adds to tn1 and then when it adds it to tn2 it
removes it from tn1, thus in the end only tn2 has the newNode and tn1 has
no
children. This surprises me. How can I add the same node to two separate
locations in a treeview?


Apr 25 '06 #4
Is this from ASP.Net 2.0? It appears that both clone and
memberwiseclone are available.
http://msdn2.microsoft.com/en-us/lib...controls.treen
ode.clone.aspx

If not, you should be able to easily derive a new component that exposes
iclonable.

However, cloning is essentially creating a new copy of an existing node.
Perhaps simple procedural logic will suffice.

private sub CreateNode(PNode as node, NodeName as string, Tag as object)

dim cNode as treenode
cNode = pnode.nodes.add(NodeName)
cNode.tag = Tag

end sub

Private sub CloneNode(PNode as treenode, CNode as treenode)

dim newNode as treenode

newNode = pNode.nodes.Add(cnode.text)

with newNode
.Tag = CNode.Tag
'Add other property states here
end with

End Sub

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Apr 26 '06 #5

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

Similar topics

1
by: Dmitry Karneyev | last post by:
Hi! How to add tooltip to a TreeView node? Thanks for any advice! Dmitry.
1
by: Abdessamad Belangour | last post by:
Hi all, I have a Windows Form class containing a treeView control. I have another class HierarchyDocument that creates a hierarchy of treeNodes and has a method (createHierarchy) that returns...
9
by: James Geurts | last post by:
Hey all... I posted this in the vs.net ide group too, but people are not answering, so I figured that it might be more appropriate here. I'm not sure if I'm adding a designer to my code properly. ...
3
by: markaelkins | last post by:
Hi. I am trying to enter a variable in the treenodesrc of a treenode. I am basically trying to send an ID variable into sql to return different records. I've searched everywhere and cannot find the...
4
by: | last post by:
I need to add a string property to a TreeNode object. One way I can do this is to use the tag property. Here is my question, can I do it this way, create a new class e.g. MyTreeNode which inherits...
3
by: tanya foster | last post by:
Hello, I am re-writing a visual basic .net application(visual studio 2003) in an asp.net application(visual studio 2005). The vb.net application relied on a treeview and hence, treenodes. The...
1
by: Bart Steur | last post by:
Hi As a VB6 user I'm unfamiliar with the Treeview control of VB2005. I read it was changed but I have a hard time finding the right help/samples for my problem. In VB6 you had 1 collection,...
1
by: AAaron123 | last post by:
I have the following in a .css file. The treeNodes behave as if they were "a" elements. I can't get them to not respond to the mouse! The treenodes are in a content file and the link is in...
1
by: AAaron123 | last post by:
If you see this posted twice - sorry. My news reader showed my first post as "No Longer Available" I have the following in a .css file. The treeNodes behave as if they were "a" elements. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
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,...
0
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...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.