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

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?
Who is Participating?
Priest04Connect With a Mentor Commented:
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

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.

rosenavAuthor Commented:
So I should just accept that the C# library is incomplete and rewrite my context menus?
rosenavAuthor Commented:
Thanks for your help. I think I'll have to rewrite with ContextMenuStrip when I get a chance.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.