0

It is my understanding that the NAME of a node in a TreeView is the same as the KEY. If so, I don't understand why ContainsKey (fyi: ContainsKey requires string) does not return the correct result:

MessageBox.Show(tv_Projects.SelectedNode.Name); //This shows "1"
MessageBox.Show(tv_Projects.Nodes.ContainsKey("1").ToString()); //This shows "false"

Why does the second message box say "false" when the key "1" clearly exists, as stated in the first message box. This is how I create the node.

TreeNode Approved;
TreeNode Approved_Open;
Approved = TV_Project.Nodes.Add("Approved Projects");
Approved_Open = Approved.Nodes.Add("Open");
Approved_Open.Nodes.Add(dr["Project ID"].ToString(), dr["Project Name"].ToString());

Project ID and Project Name are pulled from a database. Project ID returns 1 as proven in the first message box.

Any help is greatly appreciated. Thanks!

IceBurger
  • 155
  • 1
  • 8
Daniel227
  • 127
  • 9
  • 1
    It will return `true` if the `tv_Projects.SelectedNode` belongs to `tv_Projects.Nodes` collection. And will return `false` if, for instance it belongs to `tv_Projects.Nodes(0).Nodes`. So to get a specific node in the TreeView control, you need to use a recursive function to iterate through the branches and get that node by the `Name` or `Tag` properties. –  Nov 25 '19 at 03:51
  • Thanks @JQSOFT, do you happen to have a link that describes the recursive function or alternatively, can you please explain on how to add the selected node to the tv_projects.nodes collection? – Daniel227 Nov 25 '19 at 04:41
  • Please check [How to Iterate through all nodes of a treeView Control. C#](https://stackoverflow.com/questions/19691286/how-to-iterate-through-all-nodes-of-a-treeview-control-c-sharp) and [Get a list of all tree nodes (in all levels) in TreeView Controls](https://stackoverflow.com/questions/4702051/get-a-list-of-all-tree-nodes-in-all-levels-in-treeview-controls/24856090) and many more. –  Nov 25 '19 at 04:48

0 Answers0