?
Solved

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

Posted on 2009-04-25
11
Medium Priority
?
1,119 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:gs_kanata
  • 4
  • 4
  • 2
10 Comments
 
LVL 3

Expert Comment

by:Aleksei_Malkov
ID: 24231761
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
 

Author Comment

by:gs_kanata
ID: 24231942
Aleksei_Malkov:

Thanks for your quick response, but it does not work. The "selected" variable out of your for loop is null.
0
 
LVL 3

Expert Comment

by:Aleksei_Malkov
ID: 24232074
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 24232589
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
 
LVL 3

Expert Comment

by:Aleksei_Malkov
ID: 24232732
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
 
LVL 3

Expert Comment

by:Aleksei_Malkov
ID: 24232737
Sorry, I've meant TreeView.AfterSelect event, not the element
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 24232738
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
 

Author Comment

by:gs_kanata
ID: 24232771
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 24232797
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
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 24232855
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses
Course of the Month17 days, 7 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question