Create a Dynamic Menu Using C#

Create a Dynamic Menu Using C#

Introduction

There have been articles on the technique of creating MRU (Most Recently Used) file menus in Visual Studio environment, for example http://www.codeguru.com/csharp/csharp/cs_misc/designtechniques/article.php/c15571. In my recent project, I need not only to create a MRU file menu, but also other menus that are dynamically created. For example, a user-customizable tools menu, help menu, and so forth. This article generalizes the way of creating a MRU menu to introduce the concept of a Dynamic Menu, which is created dynamically at application execution time.

One thing in common in the menus my application needs to create is that each dynamic menu item is associated with data. In the case of a MRU file menu item, it is the file path to be opened, and in the case of a tools menu item, it is the path of an application to be invoked. When it comes to a user-customizable help menu, it is a document file of various types to be displayed. In all these cases, the data associated with a menu item is of string type. It does not prevent you to make it an object to be more general. The MenuItem class in System.Windows.Forms namespace has the object type property Tag that can be used to store user data. You can utilize this placeholder to store the data of dynamic menu items, but I prefer to derive a class from the MenuItem item class. This allows you to further expand this class in the future when needed, and also it makes the code easy to understand. In my example code, the derived class is named DynamicMenuItem. To simplify the code, I make this data member string type, which meets the needs of MRU file menu and tools menu.

To add dynamic menu items to a menu list, a dynamic menu manager class is designed. The manager class controls the type of dynamic menu list and the way to add a menu item to the menu list that I will explain in detail.

The Dynamic Menu Item Class

As has been discussed, the dynamic menu item class is derived from MenuItem class.

Anchor Menu Items, Dynamic Menu Types, and Dynamic Menu Manager

An anchor menu item is an menu item added at menu design time. The purpose of an anchor is to position the dynamic menu item list. The dynamic menu item list may be at the same level of the anchor, which I call the inline dynamic menu item list. The list is immediately below the anchor. The dynamic menu item list also can be a submenu of the anchor. In the case of an inline menu list, the anchor should be invisible. The separator immediately above it or below it should also be invisible until there is one dynamic menu item added. If the menu list forms a submenu of the anchor, the anchor is visible but is disabled until there is one dynamic menu item added. The separator is always visible in this case. The type of dynamic menu type and the visibility of the anchor is controlled by DynamicMenuMgr. When a new dynamic menu item is added to the menu list, it can be put on top of the list or be appended to the list. The manner of adding a new menu item is also controlled by DynamicMenuMgr.

When a new menu item is added, DynamicMenuMgr is responsible for registering the menu item click event handler. All the dynamic menu items share a common event handler. In my demo code, the event handler displays the data stored in the menu item being clicked in a message box.

Create a Dynamic Menu Using C#

Following is the code for the DynamicMenuMgr class. I have kept it as simple as possible as long as it meets the needs of my demo project and it illustrates the basic idea. You can write a class derived from DynamicMenuMgr to extend the capabilities. For example, you can add code to retrieve menu text and data from a source such as the system Registry, or a database table or XML file. You also need to rewrite the dynamic menu item click event handler to do real things, such as open a file.

Top White Papers and Webcasts

When individual departments procure cloud service for their own use, they usually don't consider the hazardous organization-wide implications. Read this paper to learn best practices for setting up an internal, IT-based cloud brokerage function that service the entire organization. Find out how this approach enables you to retain top-down visibility and control of network security and manage the impact of cloud traffic on your WAN.

U.S. companies are desperately trying to recruit and hire skilled software engineers and developers, but there is simply not enough quality talent to go around. Tiempo Development is a nearshore software development company. Our headquarters are in AZ, but we are a pioneer and leader in outsourcing to Mexico, based on our three software development centers there. We have a proven process and we are experts at providing our customers with powerful solutions. We transform ideas into reality.