I've got a treeview control with dynamically created node/ childnodes. When the user clicks on a particular node, a table display should be updated with some relevant node information/settings. To track which node is being clicked I added a custom tag (a class called 'NodeTag' containing two fields) to the node after it had been created run-time. I've subscribed to the 'AfterSelect' event of the treeview control and I'd like to check the SelectedNode.Tag but strangely I'd always got the last tag object for all selected objects: all nodes have NodeTag.Nodename = "GLBS" and NodeTag.NodeNumber = 0. Here is a pic, where 'Current Session' is selected, the selected node text is correct but the tag is not.
Here is the code:
TreeNode SessionNode = new TreeNode("Current Session");
//Add Tag object to the new node
NodeTag nt = new NodeTag();
nt.NodeName = "SESS";
nt.NodeNumber = 0;
SessionNode.Tag = (object) nt;
tv_project.Nodes.Add(SessionNode);
MainProject.ProjectNode = new TreeNode("Project - " + e.ProjName + " - " + e.ProjOwner);
//Add Tag object to the new node
nt.NodeName = "PROJ";
nt.NodeNumber = 0;
MainProject.ProjectNode.Tag = (object) nt;
SessionNode.Nodes.Add(MainProject.ProjectNode);
TreeNode SettingsNode = new TreeNode("Global Settings");
//Add Tag object to the new node
nt.NodeName = "GLBS";
nt.NodeNumber = 0;
SettingsNode.Tag = (object) nt;
SettingsNode.ForeColor = Color.Red;
MainProject.ProjectNode.Nodes.Add(SettingsNode);