How to get the highlighted tree node even it has lost focus?

I have a tree view and navigate through different nodes. Then I stop at one node and it gets highlighted. Now I want show more information about it in another windows control saying listview. So I create a new menu item to let users to view more details for that node. However, when the menu item is seleted and inside the menu selection event handler, the tree view has no item selected.

BTW, I have set "HideSelection" property for treeview to false.
private void viewSelectedProductToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
            try
            {
                MessageBox.Show(productTreeView.SelectedNode.Text);
            }
            catch (NullReferenceException)
            {
                MessageBox.Show("No item is selected in student tree view");
            }
        }

Open in new window

gs_kanataAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Aleksei_MalkovCommented:
Try to check if the node is selected at all through Nodes  collection


TreeNode selected = null;
foreach(TreeNode node in productTreeView.Nodes)
{
   if (node.IsSelected)
   {
       selected = node;
       break;
   }
}

Open in new window

0
gs_kanataAuthor Commented:
Aleksei_Malkov:

Thanks for your quick response, but it does not work. The "selected" variable out of your for loop is null.
0
Aleksei_MalkovCommented:
Yes, It is set to null outside the loop. And then we start loop to find out what node is selected. Oops, I've forgotten to make a recursion about the nodes, in my source code I enumerate only top level... New source code enclosed
    TreeNode selected = GetSelected(productTreeView.Nodes);
    if (selected == null)
        MessageBox.Show("No nodes selected");
 
    // function
    public TreeNode GetSelected(TreeNodeCollection collection)
    {
        foreach (TreeNode node in collection)
        {
            if (node.IsSelected)
            {
                return node;
            }
            TreeNode child = GetSelected(node.Nodes);
            if (child != null)
                return child;
        }
        return null;
    }

Open in new window

0
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Create a CLASS level variable to hold the currently selected TreeNode.  Then, in the MouseDown event (before the Menu is displayed), set the variable to the current node.  Now you can access that variable in your menu code...
0
Aleksei_MalkovCommented:
It is not suggested to use Mouse Down becuase there are lots of ways to select node in the tree. If store the selected node in variable use TreeView.AfterSelect element to store the variable
0
Aleksei_MalkovCommented:
Sorry, I've meant TreeView.AfterSelect event, not the element
0
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
True...it sounded like the menu was being invoked by right clicking on a node like a context menu though.   I could be wrong!  =)
0
gs_kanataAuthor Commented:
Let me give more detailed info here:

I have one Main form as the MDI parent and several forms as the MDI children. The Main form has the menu items. By click one menu item, one MDI child form open which as tree view. The user can walk through the tree and stop on a node. Then the user can go back to parent main form menu to choose another menu item to view the detailed info of the node which the user stopped at/hightlight. So the menu item selection even handler is at the main parent form code. In the main form code, it can get the reference of the tree view on the child form. However, during the runtime debugging, I have found that the treeview reference node count is 0. How could? Is that reason I could not locate the selected node?
0
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
It sounds like you may not be getting a reference to the correct Form/TreeView...

Can we see how you are getting your references in the MdiParent code?
0
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
I was able to get it to work fine using this code in the MdiParent:

        // ...being run in the MdiParent...
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            Form frm = this.ActiveMdiChild;
            if (frm is Form1)
            {
                Form1 f1 = (Form1)frm;
                TreeNode tn = f1.treeView1.SelectedNode;
                if (tn != null)
                {
                    Console.WriteLine(tn.Text);
                }
            }
        }

My TreeView was on Form1 and I changed the Modifiers() Property of the TreeView to Public so I could see it from the MdiParent Form.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.