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?
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
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
 
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
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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 TomlinsonMiddle School Assistant TeacherCommented:
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 TomlinsonMiddle School Assistant TeacherCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.