[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Collapse event in C# ContextMenu does not work as expected.

Posted on 2008-11-05
Medium Priority
Last Modified: 2012-08-13
I am trying to change the state of my application according to whether or not a ContextMenu is being displayed.

ContextMenu menu = new ContextMenu();
menu.Popup += new EventHandler( doSomething );
menu.Collapse += new EventHandler( doSomethingElse );
menu.Show(this, new Point(e.X, e.Y));

doSomething gets called as expected whenever a menu is shown but doSomethingElse is never called. I also tried to subclass ContextMenu and override the popup and collapse methods themselves. Again, the popup method worked but the collapse one was never called.

Are my expectations faulty or is it the code?
Question by:rosenav
  • 2
  • 2
LVL 18

Expert Comment

ID: 22898418
May I ask why don't you use ContectMenuStrip class (since ContextMenu class is obsolete). Since you are using collapse event, this method didnt exist prior to framework 2.0, so there in no point in not using ContextMenuStrip.


Author Comment

ID: 22922669
So I should just accept that the C# library is incomplete and rewrite my context menus?
LVL 18

Accepted Solution

Priest04 earned 2000 total points
ID: 22923142
So, you are converting your 1.x application to 2.x (or 3.x)? I thought you are making a new project. Anyway, I would recommend rewriting it, if there isn't much work to do. If not, then all you can do is next:

Do not call Show method, Collapse method doesn't work with it. Instead, assign ContextMenu instance to a control. Example

this.ContextMenu = contextMenu1; // this will show context menu when you right-click on a form, and it will call Collapse event when closing it.

Open in new window


Author Closing Comment

ID: 31513622
Thanks for your help. I think I'll have to rewrite with ContextMenuStrip when I get a chance.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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.…
Screencast - Getting to Know the Pipeline
Suggested Courses

872 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