views:

33

answers:

1

I have a contextmenuStrip associated with a tree view.

Now for instance, i have four nodes in the tree structure and Node 4 is selected.

Behavior: ContextMenuStrip - When u right click on the Node 2 , that node is selected and as soon as the context menu strip opens up, the focus goes back to Node 4.

With old component 'Context Menu' this functionality works fine i.e. Node 2 has the focus till the time context menu is open.

I would like to have Node 2 selected as long as context menu is open. And selection/focus shall go back to Node 4 when context menu is closed.

Request please advice.

Thanks and Best Regards Sumit

+1  A: 

Yes, the TreeView control is pretty flaky when the focus is changed while one of its events runs. Which is one reason it distinguishes the BeforeXxxx and AfterXxxx events. Unfortunately, the context menu strip is shown too soon. The solution is to display the context menu yourself by implementing the NodeMouseClick event. Like this:

    private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) {
        if (e.Button == MouseButtons.Right) {
            treeView1.SelectedNode = e.Node;
            contextMenuStrip1.Show(treeView1, e.Location);
        }
    }

I'll leave restoring the focus afterwards up to you. It doesn't make much sense to implement it.

Hans Passant
Thanks for the feedback.
Sumit
Do you need anything else? If not, please close your thread by marking it answered.
Hans Passant