Figure 1: You can put any group of controls on the pop-up context menu.
Introduction
Long time ago, I wondered how Microsoft Access put an Edit control on the context menu! Then I got another idea ... why don't we use any control(s) on the context menu? This will make the user interface easier to use, learn and increase productivity. The good news here is that, you can make it the same way you design a user control or a form using the Visual Studio designer.
Background
At first, I thought we could customize the Menu. Then I discovered that this is very hard and maybe impossible. I tried to make use of the Form, but this is not the right choice because when we show it, the parent form loses its focus. A context menu shouldn't cause that. Luckily, I found the very good article "Simple Pop-up Control" by Lukasz Swiatkowski. This article shows how to display a pop up control using the ToolStripDropDown class. Then I said, "I found it." I used that idea to make my idea: a context menu that contains any group of controls.
How it Works
Internally SuperContextMenu uses another class PopItUp which based on ToolStripDropDown which is responsible to show the user control as a pop up control.
Using the Code
- Copy the file NewInterfaceGuides.cs into your project folder. Then add it to your project. And build the application.
- Add a new user control to your project, and then change the base class from
UserControl to NewInterfaceGuides.SuperContextMenu.
- Design that user control any way you like it using Visual Studio Designer, but make the control members public.
- In the form add a member of that user control.
- In the form constructor, after
InitializeComponent() initialize it and add event handlers to the controls.
- To show the context menu, use
ShowAsContextMenu() method
- Respond to the events handlers that fired from the controls on the menu to achieve your program functionalities
- If the user finished working with the menu (like clicked a button) and you want to close the menu, you can close it easily by calling its
Close() method.
Known Bugs
I still have these bugs but I am working on them so if you have an idea how to solve these bugs, please, help me.
- If the pop-up menu is shown and the user clicks a MenuStrip, a ToolStrip or an item on them, it does not hide. It needs two clicks to hide!
- The
System.Windows.Forms.ComboBox class behaves in a strange way. It closes the container when the user clicks on the part of the combo box's dropdown list that sticks out of the container area. So, make sure that the dropdown list is totally inside the area.
Suggested Add-on Features and Ideas
- Add a small x button to close it.
- Easily move it.
- Allow resizing.
(But I don't recommend those changes, because they violate the concept of a menu; but you may find it a good idea.)
- You can use it to implement custom tool tip, intellisense menus like visual studio.
History
- June 2008: completely changed the structure to make it easier to use. Solved the tab bug.
- December 2007: first release.