Link to home
Start Free TrialLog in
Avatar of gs_kanata
gs_kanataFlag for Canada

asked on

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

Avatar of Aleksei_Malkov
Aleksei_Malkov
Flag of United States of America image

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

Avatar of gs_kanata

ASKER

Aleksei_Malkov:

Thanks for your quick response, but it does not work. The "selected" variable out of your for loop is null.
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

Avatar of Mike Tomlinson
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...
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
Sorry, I've meant TreeView.AfterSelect event, not the element
True...it sounded like the menu was being invoked by right clicking on a node like a context menu though.   I could be wrong!  =)
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?
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?
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial