C# WinForms: Alternating between ContextMenuStrips

Posted on 2012-09-20
Last Modified: 2012-09-21
I have defined three ContextMenuStrips, one for each of three node types.  But I use all three node types in each of three TreeViews.  So I think I need to programatically switch the ContextMenuStrip upon a right mouse click, then allow the context menu to open normally.

Am I seeing this right?

If so, I would need to:

1. Create my own class which inherits from TreeView
2. Override this event:
        protected override void OnNodeMouseClick(TreeNodeMouseClickEventArgs e)
3. Find if it's a right mouse click?? (how do I do this since the event I am given is TreeNodeMouseClickEventArgs)
4. Set the ContextMenuStrip property of the TreeView
5. What next?

I am pretty sure if I did this, the context menu would not open.

Can I open it manually?
Question by:newbieweb
    LVL 74

    Assisted Solution

    by:käµfm³d 👽
    how do I do this since the event I am given is TreeNodeMouseClickEventArgs
    TreeNodeMouseClickEventArgs has a Button property which you can inspect to see which button was clicked.


    if (e.Button == MouseButtons.Right)
        // do something

    Open in new window

    I think button click might be too late in the event sequence to change the CM. You might try MouseDown--even then, I'm not sure if you'll beat the CM showing.
    LVL 25

    Accepted Solution

    The context menu will open right. But you don't even need to override the OnNodeMouseClick event, you only need to control the TreeView's NodeMouseClick event. Anyway, it's on your own, it will work right in both scenarios.

    To manually open a ContextMenuStrip, you can do the following:

    Open in new window

    Hope that helps.
    LVL 85

    Assisted Solution

    by:Mike Tomlinson
    Agreed.  I'd skip #4 entirely:

        4. Set the ContextMenuStrip property of the TreeView

    Leave the ContextMenuStrip property unassigned, and then display the ContextMenuStrip of your choosing as Roland describes based on the node type that was clicked.

    Author Closing Comment


    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
    A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
    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.…
    This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

    745 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now