1
votes

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.

Current Session selected as node

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);
1
Why in the first section (when name = SESS) you convert the nt object to Object before you insert it ti the ode's tag, but in the third section (name = GLBS) you do not do so?user3165438
My fault, corrected in my post. Still not working.JustGreg
What are the results when you debug? What is the number value of SESS node right after you initialize it, after creation of the PROJ node and after creation of the GLBS node?user3165438
After initalizing: - SessionNode {Text = "Current Session"} System.Windows.Forms.TreeNode Tag after adding it tag: correct, with 'SESS' and 0. First node null. MainProject.ProjectNode is also correctly created with its tag ('PROJ',0) etc. However, when added to SessionNode, SessionNode's Tag property is updated to ('PROJ',0).JustGreg
You have a '+' next to the node in the 'Watch' pain. A click on it will open the node properties values. There is another '+' next to the Tag property. A click will show the name and number value of the node. This is the values to check. Please check it. I'm here to help.user3165438

1 Answers

0
votes

I have the solution!

You create an instance of NodeTag just at the first time, then you just modify it .
You have to create a separate nt for each node:

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
        NodeTag nt1 = new NodeTag();
        nt1.NodeName = "PROJ";
        nt1.NodeNumber = 0;
        MainProject.ProjectNode.Tag = (object) nt1;
        SessionNode.Nodes.Add(MainProject.ProjectNode);


        TreeNode SettingsNode = new TreeNode("Global Settings");
        //Add Tag object to the new node
        NodeTag nt2 = new NodeTag();
        nt2.NodeName = "GLBS";
        nt2.NodeNumber = 0;
        SettingsNode.Tag = (object) nt2;
        SettingsNode.ForeColor = Color.Red;
        MainProject.ProjectNode.Nodes.Add(SettingsNode);