Windows Develop Bookmark and Share   
 index > Windows Forms General > tree view (help)
 

tree view (help)

ParentNode1
- my new node


i could add a new node using

treeView1.SelectedNode.Nodes.Add("my new node"); // which i point my mouse to the ParentNode1


but how could i define the node1 without using SelectedNode or without using mouse point to ParentNode1

TreeNode node1 = this.treeView1.Nodes.Contains("ParentNode1"); // this line got error ... so how to define it?
node1.Nodes.Add("my new node");
tiau  Friday, September 04, 2009 12:10 AM
Hi,

That error is probably because there is no node at the root level with a 'key' of "ParentNode1". Check the name/key of the nodes at the root level, and ensure you have the correct 'case' in your string.

What's happening is that treeView1.Nodes[streName] is returning null, and then you get that error when you try to reference (effectively)
null.Nodes.Add("my new code");

Assuming you have any root level nodes in the tree at all, this will add a child to the first node in the tree;

treeView1.Nodes[0].Nodes.Add("my new node");
Yort  Friday, September 04, 2009 1:22 AM
Hi tiau,

Based on my understanding, you need to find a special node in a TreeView control according to its text. After you find the node, you can add a new node to its child.

If my understanding is correct, the code in this thread can implement the find node function.
http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/bfc7c6ba-e19f-402b-a164-4d9425427e0a
You can change the code a little:
private void FindNode(TreeView treeView, TreeNode foundNode, string nodeText)
{
Stack<TreeNode> nodeStack = new Stack<TreeNode>();
foreach (TreeNode tn in treeView.Nodes)
{
nodeStack.Push(tn);
}

while (nodeStack.Count != 0)
{
TreeNode treeNode = nodeStack.Pop();
if (treeNode.Text == nodeText)
{
foundNode = treeNode;
break;
}

foreach (TreeNode tn in treeNode.Nodes)
{
nodeStack.Push(tn);
}
}
}
Once the node is find, the foundNode will hold that node. You can call foundNode.Nodes.Add to add new node.

Please feel free to tell me if I misunderstood you.

Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Monday, September 07, 2009 4:02 AM

Hi,

To do that without using the mouse and using the SelectedNode property you would need to set the SelectedNode property to the correct node first. Either I still don't understand your question, or I don't see why you'd do it that way... you don't need to use the selected node property to add a child, you just have to get a reference to the parent node anyway you can. That's why I posted the code for the GetNodeFromPath method, using that you can find the node by name without having to have the node selected.

Why do you want to use the SelectedNode property ? What is it you are trying to do ?

If you want to add a new node to ParentNode1 without selecting it, you need to 'find it in the tree first', since there can be multiple nodes called 'ParentNode1' in the tree in different branches you can't use just the one node name, you have to use a path, like "MyRoot\ParentNode1". If you want to do that, then again the code I posted before using GetNodeFromPath should work.

Yort  Friday, September 04, 2009 1:59 AM
TreeView.Nodes is a TreeNodeCollection .

This means you can use it's members to access it directly:

treeView1.Nodes["ParentNode1"].Add("my new node");
// Or:
treeView1.Nodes[0].Add("my new node 2"); // Add to first node at "root" level of tree

Reed Copsey, Jr. - http://reedcopsey.com
Reed Copsey, Jr.  Friday, September 04, 2009 12:27 AM
TreeView.Nodes is a TreeNodeCollection .

This means you can use it's members to access it directly:

treeView1.Nodes["ParentNode1"].Add("my new node");
// Or:
treeView1.Nodes[0].Add("my new node 2"); // Add to first node at "root" level of tree

Reed Copsey, Jr. - http://reedcopsey.com

i am having problem...


string strname = "ParentNode1";
treeView1.Nodes[strName].Add("my new node");


Error 1 'System.Windows.Forms.TreeNode' does not contain a definition for 'Add' and no extension method 'Add' accepting a first argument of type 'System.Windows.Forms.TreeNode' could be found (are you missing a using directive or an assembly reference?)
tiau  Friday, September 04, 2009 12:33 AM
Oops - sorry about that. I'm tired today - keep messing up... :S

When you call treeView1.Nodes[strName], you're returning a single node. To add to it's children, you need to refer to it's child nodes .

It should be:

string strname = "ParentNode1";
treeView1.Nodes[strName].Nodes.Add("my new node");



Reed Copsey, Jr. - http://reedcopsey.com
Reed Copsey, Jr.  Friday, September 04, 2009 1:12 AM
Oops - sorry about that. I'm tired today - keep messing up... :S

When you call treeView1.Nodes[strName], you're returning a single node. To add to it's children, you need to refer to it's child nodes .

It should be:

string strname = "ParentNode1";
treeView1.Nodes[strName].Nodes.Add("my new node");



Reed Copsey, Jr. - http://reedcopsey.com

it gave me an error
Object reference not set to an instance of an object.


by the way, i also tried
string strname = "ParentNode1";
TreeNode node = this.treeView1.Nodes[strName];
node.Nodes.Add("my new node");

it gave the same error "Object reference not set to an instance of an object. "
tiau  Friday, September 04, 2009 1:16 AM
Have you added ParentNode1 yet?


This should work:

treeView1.Nodes.Add(strName); // Make sure this node is here, first!

treeView1.Nodes[strName].Nodes.Add("my new node"); // Now you can add children to it
Reed Copsey, Jr. - http://reedcopsey.com
Reed Copsey, Jr.  Friday, September 04, 2009 1:22 AM
Hi,

That error is probably because there is no node at the root level with a 'key' of "ParentNode1". Check the name/key of the nodes at the root level, and ensure you have the correct 'case' in your string.

What's happening is that treeView1.Nodes[streName] is returning null, and then you get that error when you try to reference (effectively)
null.Nodes.Add("my new code");

Assuming you have any root level nodes in the tree at all, this will add a child to the first node in the tree;

treeView1.Nodes[0].Nodes.Add("my new node");
Yort  Friday, September 04, 2009 1:22 AM
Have you added ParentNode1 yet?


This should work:

treeView1.Nodes.Add(strName); // Make sure this node is here, first!

treeView1.Nodes[strName].Nodes.Add("my new node"); // Now you can add children to it
Reed Copsey, Jr. - http://reedcopsey.com

emm... actually there is anothe level above the ParentNode1

MyRoot
-ParentNode1
-- my new node


So in this case how to define it?
and wont it be better to use selectednodes in this case?
tiau  Friday, September 04, 2009 1:28 AM
Something like;

treeView1.Nodes["MyRoot"].Nodes["ParentNode1"].Nodes.Add("my new node");

Or you could build your own method to 'walk' the tree from a string, i.e

Assume you have a string "MyRoot\ParentNode1"

Write a routine that accepts a string, splits on the character \ then loops through the resulting array looking up each string in the nodes collection of the previous node,e.g something like

		private TreeNode GetNodeFromPath(string path, TreeView treeView)
		{
			TreeNode retVal = null;
			TreeNodeCollection children = treeView1.Nodes;

			foreach (string nodeName in path.Split(new char[] { '\\' }))
			{
				retVal = children[nodeName];
				if (retVal != null)
					children = retVal.Nodes;
			}

			return retVal;
		}
and then call it like this;

			TreeNode node = GetNodeFromPath("MyRoot\\ParentNode1", treeView1);
			if (node != null)
				node.Nodes.Add("my new node");
Yort  Friday, September 04, 2009 1:39 AM
Something like;

treeView1.Nodes["MyRoot"].Nodes["ParentNode1"].Nodes.Add("my new node");

Or you could build your own method to 'walk' the tree from a string, i.e

Assume you have a string "MyRoot\ParentNode1"

Write a routine that accepts a string, splits on the character \ then loops through the resulting array looking up each string in the nodes collection of the previous node,e.g something like

		private


 TreeNode GetNodeFromPath(string


 path, TreeView treeView)
		{
			TreeNode retVal = null


;
			TreeNodeCollection children = treeView1.Nodes;

			foreach


 (string


 nodeName in


 path.Split(new


 char


[] { '\\'


 }))
			{
				retVal = children[nodeName];
				if


 (retVal != null


)
					children = retVal.Nodes;
			}

			return


 retVal;
		}
and then call it like this;

			TreeNode node = GetNodeFromPath("MyRoot\\ParentNode1"


, treeView1);
			if


 (node != null


)
				node.Nodes.Add("my new node"


);

could teach me how to use selectedNodes method?

where i could add a new node by using below statement
treeView1.SelectedNode.Nodes.Add("my new node"); // which i point my mouse to the ParentNode1
but now i want the strName to be get focused in order i could use the .selectedNodes
tiau  Friday, September 04, 2009 1:47 AM
Sorry, I don't quite understand the question ? Are you saying you want to select the newly added node ? If so, then this should work;

TreeNodeselectedNode = treeView1.SelectedNode;
selectedNode.Nodes.Add("my new node");
treeView1.SelectedNode = selectedNode.Nodes["my new node"];

Yort  Friday, September 04, 2009 1:51 AM
Sorry, I don't quite understand the question ? Are you saying you want to select the newly added node ? If so, then this should work;

TreeNodeselectedNode = treeView1.SelectedNode;
selectedNode.Nodes.Add("my new node");
treeView1.SelectedNode = selectedNode.Nodes["my new node"];


i mean i could add a new node by using my mouse point to the branch that i wan to add as child by using
treeView1.SelectedNode.Nodes.Add("my new node");


but without using my mouse to point to the branch...
how am i going to add a new child to it using the .SelectedNode

string strName = "ParentNode1" //branch"
tiau  Friday, September 04, 2009 1:54 AM

Hi,

To do that without using the mouse and using the SelectedNode property you would need to set the SelectedNode property to the correct node first. Either I still don't understand your question, or I don't see why you'd do it that way... you don't need to use the selected node property to add a child, you just have to get a reference to the parent node anyway you can. That's why I posted the code for the GetNodeFromPath method, using that you can find the node by name without having to have the node selected.

Why do you want to use the SelectedNode property ? What is it you are trying to do ?

If you want to add a new node to ParentNode1 without selecting it, you need to 'find it in the tree first', since there can be multiple nodes called 'ParentNode1' in the tree in different branches you can't use just the one node name, you have to use a path, like "MyRoot\ParentNode1". If you want to do that, then again the code I posted before using GetNodeFromPath should work.

Yort  Friday, September 04, 2009 1:59 AM
Hi tiau,

Based on my understanding, you need to find a special node in a TreeView control according to its text. After you find the node, you can add a new node to its child.

If my understanding is correct, the code in this thread can implement the find node function.
http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/bfc7c6ba-e19f-402b-a164-4d9425427e0a
You can change the code a little:
private void FindNode(TreeView treeView, TreeNode foundNode, string nodeText)
{
Stack<TreeNode> nodeStack = new Stack<TreeNode>();
foreach (TreeNode tn in treeView.Nodes)
{
nodeStack.Push(tn);
}

while (nodeStack.Count != 0)
{
TreeNode treeNode = nodeStack.Pop();
if (treeNode.Text == nodeText)
{
foundNode = treeNode;
break;
}

foreach (TreeNode tn in treeNode.Nodes)
{
nodeStack.Push(tn);
}
}
}
Once the node is find, the foundNode will hold that node. You can call foundNode.Nodes.Add to add new node.

Please feel free to tell me if I misunderstood you.

Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Monday, September 07, 2009 4:02 AM

You can use google to search for other answers

Custom Search

More Threads

• get WebBrowser cached images
• Displaying a bitmap on a picture box
• windows app not topmost when launching
• ToolStripButton Click Twice
• Break WinForm into 3 equal areas
• Printing Question
• Property Grid Troubles
• C# context menu
• Emailing
• Providing Color.FromArg(int) with int for waterprint-color