开发者

ContextMenuStrip loses the focus from an item in the tree view if it is right clicked

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 o开发者_Python百科pen. And selection/focus shall go back to Node 4 when context menu is closed.

Request please advice.

Thanks and Best Regards Sumit


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜