how to replace TreeView's selected node with newly created node - c#

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

Related

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.

Question mark inside XML file without encoding produces errors while trying to load it

I am trying to get all the elements in a selected "Xpath location" and add them to a comboBox/ drop down list.
I tried to select all these elements by using the Xpath query : /#* with the Select method from the XpathNodeIterator.
The problem is that it return an iterator which does not move forward, in fact, it says it has no childs and its not allowing me to convert the iterator neither to an xmlElement or node so that I could at least search inside them.
This is the code :
while (anIterator.MoveNext())
{
//im trying to select all nodes of selected path which and return them to an iterator
secondIterator = anIterator.Current.Select("/#*");
while (secondIterator.MoveNext())
{
aNode = new Nodes();
aNode.Name = anIterator.Current.MoveToFirstChild().ToString();
nodeList.Add(aNode);
}
nodeList.Add(aNode);
}
Any solution to get all the elements in the current node ?
This is definitely harder to answer without the XML file you're working with, and without you telling us what you're hoping to see, and what you're currently seeing.
Here are some observations:
The '#' character in your XPath suggests you're trying to get all of the attributes of the current Node. The '/' tells Path to start at the root of the DOM.
If you're trying to get all the elements under the current node, you might want to change to "./*"
The issue is the #, which indicates that you want to select the child attributes, not child elements. (See http://www.w3.org/TR/xpath/#path-abbrev).
Try anIterator.Current.Select("*") to get the child elements. If you want all descendant elements (not just immediate children), try anIterator.Current.Select(".//*")

Problem with adding nodes to a treeview

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

What is the best method to loop through TreeView nodes and retrieve a node based on certain value?

What is the best method to loop through TreeView nodes and retrieve a node based on certain value?
It would more efficient to create something like a Dictionary<string, TreeNode> and add all the nodes in it. This of course must be done at the start of the form or whenever you add new tree nodes. The dictionary key can be anything e.g TreeNode Text or business object associated with the node.
You won't need to traverse through all the nodes - just use the search criteria (key) and retrieve the node.

Categories

Resources