Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


C# WinForms: I need the right mouse click to select a TreeView node

Posted on 2012-09-13
Medium Priority
Last Modified: 2012-09-13
I have a working TreeView and context menu which comes up when the right mouse button is clicked on various nodes and node types.  Fine.

But, if I click on a node which is not selected, the context menu pops open but the selection jumps back to the previously selected node.  This is errant behavior I need to fix.

I want the right click to select that node.

Any suggestions?

I would think this would work:

        internal void TvMainMouseUp(MouseEventArgs e)
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
                _view.TreeViewRules.SelectedNode = _view.TreeViewRules.GetNodeAt(_view.TreeViewRules.PointToClient(Cursor.Position));

but it doesn't
Question by:newbieweb
  • 2
  • 2
LVL 86

Accepted Solution

Mike Tomlinson earned 2000 total points
ID: 38397175
Set it in the Opening() event of your ContextMenuStrip:
        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
            TvMain.SelectedNode = TvMain.GetNodeAt(TvMain.PointToClient(Cursor.Position));

Open in new window


Author Closing Comment

ID: 38397214
That sounds believable enough.

LVL 86

Expert Comment

by:Mike Tomlinson
ID: 38397226
Haha!...I tested it first.  ...but that was a simple case scenario.  Let me know if it doesn't work for any reason...

Author Comment

ID: 38397237

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Screencast - Getting to Know the Pipeline

564 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