Afternoon all!

Was hoping for a bit of help with something.


I am oringally a C++ developer so this way of thinking may be way out there when coding in c# but I thought I would ask the experts first.


I am currently creating an XML Viewer. I have a TreeView with a Collection of TreeNodes holding all the Elements within the Xml document. I also have an option to add tabs which will open a new tab with the Main TreeView's selected Node as the root node of a new TreeView.

At the moment I am cloning the TreeNode when assigning it to the new TreeView, but I was wondering if there is some way of just making a pointer to the original Treenode and assigning that pointer to the new TreeView?


Thanks in advance for your help!!


Solemn.

Recommended Answers

All 5 Replies

Remember everything is a reference which is a lot like a pointer from C/C++ (you can't do pointer math and a few other things). So something like this makes both variables reference the same object. You'll just need to do the proper assignments for your TreeNode/TreeView.

Object A = new Object();
Object B = A;

Ok, but when I try and add the TreeNode which is a Refference to the my TreeViewMain.SelectedNode, I get this error

Cannot add or insert the item 'parent' in more than one place. You must first remove it from its current location or clone it.

TreeNode refNode= new TreeNode();
            refNode = tv_Master.SelectedNode;
            newTView.Nodes.Add(refNode);

You can see C++ thinking going on in that sample. Still, according to MSDN on TreeNode.Parent the parent property returns a single value and is get only.

Try the following (written from memory so YMMV):

TreeNode refNode = tv_Master.SelectedNode;  // no need to assign memory with new, just create the reference
TreeNode newRefNode = refNode.Clone();  // note: this will clone the tree from this point down
newRefNode.Tag = refNode;  // TreeNode.Tag is a useful way of cross referencing things

is was possible to a MasterTreeView with TreeNodes in it and then a ChildTreeview with Refferences to the Treenodes from MasterTreeView

To try and make it alittle more clear as to what it is I am trying to do

TreeView tv_Master = new TreeView();
TreeView tv_Child = new TreeView();


//Hardcoded assigned nodes
TreeNode node1 = new TreeNode();
TreeNode node2 = new TreeNode();
TreeNode node3 = new TreeNode();
TreeNode node4 = new TreeNode();
TreeNode node5 = new TreeNode();

node1.Nodes.Add(node2);
node1.Nodes.Add(node3);

node2.Nodes.Add(node4);
node2.Nodes.Add(node5);


//Setup Master TreeView
tv_Master.Nodes.Add(node1);
tv_Master.SelectedNode = node2;


//Point Child TreeView to Master TreeView's SelectedNode
tv_Child.Nodes.Add(node2);


//Reasoning..
tv_Master.SelectedNode.Text = "Changed";

Now node2's text is changed and it will be displayed on both the child and the master nodes.

Of cause this wont work because you cant have node 2, node4 and node5 in more than one collection (i believe at least) so I'm kinda looking for a work around, if it is at all possible.

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.