Problem with adding nodes to a treeview - c#

When I was trying to copy node from one treeview to another, I got a strange error: "Cannot add or insert the item 'node1' in more than one place. You must first remove it from its current location or clone it.
Parameter name: node"
After searching for a while, I couldn't find any solution. I tried this in VB.NET and had the same error
Code example:
TreeNode node1 = new TreeNode("node1");
node1.Name = "node1";
treeView1.Nodes.Add(node1);
TreeNode nd = treeView1.Nodes[0];
treeView2.Nodes.Add(nd);
Are there any solutions for this?
Thanks everyone! This works now!

yes ,use deep copy
TreeNode nd = (TreeNode )treeView1.Nodes[0].Clone();
change your code to this
TreeNode node1 = new TreeNode("node1");
node1.Name = "node1";
treeView1.Nodes.Add(node1);
TreeNode nd = (TreeNode )treeView1.Nodes[0].Clone(); // clone the object
treeView2.Nodes.Add(nd);

Have a look at TreeNode.Clone Method
Also from TreeNodeCollection.Add Method (TreeNode)
A TreeNode can be assigned to only one
TreeView control at a time. To add the
tree node to a new tree view control,
you must remove it from the other tree
view first or clone it.

in here:
TreeNode nd = treeView1.Nodes[0];
you are assigning the node node1 to the nd reference.
when you later add nd to the other TreeView you get the error because node1 is already bound to the other TreeView.
if you really need to do this, you should copy/close the node and not simply reference it as you are doing right now.

You are trying to add the same node into 2 different treeviews
TreeNode nd = treeView1.Nodes[0]; //make nd reference treeView1.Nodes[0]
treeView2.Nodes.Add(nd);// add treeView1.Nodes[0] into treeView2

Related

Navigate between XML Elements with Same Attribute

I'm working on a C#/ASP.Net project.
Let's say this is an xml document:
parent1
child1 attributeA
child2 attributeA
parent2
child3 attributeA
child4 attributeB
I want to navigate with next and previous buttons between anything with attributeA, so if I'm at parent1/child2, next would be parent2/child3 and previous would be parent1/child1.
I can create a new XML Document, I can load it, and I can get the current node, but I don't know about next and previous.
How can I do that? Haven't done xpaths in a while. A LONG while. I looked around here for something similar but either it's not there or I can't find it.
Can anyone help?
The MSDN has a nice article about XPaths with great examples
But this code should give you all the nodes that have attributeA regardless of where they are nested in the XML:
var doc = new XmlDocument();
doc.Load(#"C:\path\to\file.xml");
XmlNodeList nodes = doc.SelectNodes("//*[#attributeA]");
foreach (var node in nodes)
{
// your code here
}
The path //*[#attributeA] boils down to:
// "one or more levels deep"
* "any element"
[#attributeA] "with attribute 'attributeA'"

C# add XMLNode to XMLNodeList

This one I thought would be simple, but i really just have 1 node I need to add to a node List.
So I have XmlNode xmlNode. This node has all the information I need in it. I need to add this to XmlNodeList xmlnodeList. I know this is dumb because it's not a list, but the following code has all the parts to handle a node list and would be a pain to unwind.
Also normally I would use XmlNodeList xmlnodeList = xmlNode.FirstChild.SelectNodes however i need this entire node, not just the childnodes.
Thanks!
How about:
XmlNodeList xmlNodeList = xmlNode.SelectNodes(".");

c# how to link 2 LinkedListNode?

I created a Linked list and a few nodes, I want to link those node, kept getting this error message.
"
Property or indexer System.Collections.Generic.LinkedListNode<>.Next cannot be assigned to it is read only.
"
var link = new LinkedList<int>();
var node1 = new LinkedListNode<int>(1);
var node2 = new LinkedListNode<int>(2);
var node3 = new LinkedListNode<int>(3);
link.AddFirst(node1);
link.AddFirst(node2);
link.AddFirst(node3);
node1.Next = node2; ---> .next is read only
node2.Next = node3; ---> .next is read only
You need to use the AddAfter or AddBefore methods of the list. With these you can insert items directly before or after a given item.
Unfortunately the LinkedListNode<T> class in .NET does not allow you to change the Next and Previous properties directly, since they don't have a set accessor.
If you want to change the ordering of the list, you'll also need to use the Remove method to remove the item from its previous position. I recommend the following form:
LinkedListItem<T> foo = /*fetch your item here*/
LinkedListItem<T> bar = /*the one to come right after foo,
as a result of running the code*/
list.Remove(foo);
list.AddBefore(bar, foo);
You could change that to insert after instead of inserting before.
You don't have to link them. When you invoke the AddFirst method, it automatically links them to the first node which then becomes the second node.
You're trying to add items to other items, but you should be adding them to the LinkedList itself, which you're already doing:
link.AddFirst(node1);
link.AddFirst(node2);
link.AddFirst(node3);
Are you trying to change their ordering? It looks like that can only be done when adding them, based on the methods available on LinkedList<T>. So you would want to do something like this instead:
link.AddFirst(node1);
link.AddAfter(node1, node2);
link.AddAfter(node2, node3);
Or, in your case, simply reversing the order of the calls to .AddFirst() will produce the same results:
link.AddFirst(node3);
link.AddFirst(node2);
link.AddFirst(node1);
This would add node3 to the start of the list, then add node2 to the start of the list (pushing node3 to the second element), then add node1 to the start of the list (pushing node3 to the third element and node2 to the second element).

Get Collection after traversing in Umbraco

Here is my Project Folder Structure into Tree Structure
(*)Root
[]English
[1]Novel
[2]Thriller
[2.1]Happy
[2.1.1]Life Happy
HappyInLife
LoveInLife
[2.1.2]Joy
everywhereJoy
[2.1.3]Lauging
Always
Sometimes
Never
[2.2]Sad
[2.3]Excited
[2.4]Alone
[3]Love Story
[4]Action
[]Hindi
[]Marathi
Now here in the above structure I am at node [2.4]Alone
and I want to traverse at node [2.1]Happy. Further I want to access all children , sub children and sub .... of node [2.1] Happy.
My Project requirement is to display the node [2.1]Happy along with all its children and sub children and so on.... from the node [2.4]Alone
My Work up till now (I am at node [2.4]Alone)
var home=#Model.Parent.Children.First();
So home will have the node [2.1]Happy inside it.
but the problem is it will give me only the children of [2.1]Happy and not its Children's children and so on...
I tired using #Model.AncestorsOrSelf(3);
but not able to achieve my target .
Any help is appreciated
I suggest using uQuery: (1) (2)
Code would look like:
#using umbraco
var current = umbraco.NodeFactory.Node.GetCurrent();
var your21Happy = current.Parent.GetChildNodes().First(); // or the way you was getting it already.
var descendants = your21Happy.GetDescendantNodes();
uQuery is pretty flexible, you can filter queried nodes by anything, like
.GetDescendantNodesByType("uBlogsyPost")
or
.GetDescendantNodes(x => x.WriterName == "admin")
etc.

how to replace TreeView's selected node with newly created node

I have a populated treeView with Node I created, there are several node classes, all inherit from treeNode.
When i edit a node (using a GUI dialog), it may change to different class, so I'm creating a new node in that process, and trying to replace the selected node with my new node, but that doesn't work, the node stays the old one, cant figure out what i'm doing wrong.
Code:
TreeNodeMission mission = (TreeNodeMission)treeView.SelectedNode;
TreeNodeMission newMission = ChangeMissionDialog(mission);
treeView.SelectedNode = newMission; // doesn't work
Also tried removing and adding it, also doesn't work
index = treeView.Nodes.IndexOf(treeView.SelectedNode); // index returns -1
treeView.Nodes.Remove(treeView.SelectedNode);
treeView.Nodes.Insert(index, newMission);
What am i doing wrong?
Update:
treeView.SelectedNode is not null, its a valid node i selected.
Solved it, found the bug.
I found a way to replace the node, by removing and re-adding it.
I guess i thought asking for index will give me general index in the tree, but it gives index to the parent only, so using the parent node, i can replace it:
int index = treeView.SelectedNode.Index;
treeView.SelectedNode.Parent.Nodes.RemoveAt(index);
treeView.SelectedNode.Parent.Nodes.Insert(index, mission);
treeView.SelectedNode = mission;
Thanks

Categories

Resources