Click here to Skip to main content
6,634,665 members and growing! (19,469 online)
Email Password   helpLost your password?
Desktop Development » Menus » Menus and Toolbars     Intermediate License: The Code Project Open License (CPOL)

MenuDecorator - Helper Class to Diagnose your Main Menu Contents

By Jaime Olivares

A class to colorize and enable the implemented main menu options in one step
C# 2.0, Windows, .NET 2.0VS2008, Dev
Posted:4 Jun 2007
Updated:30 Jun 2008
Views:22,058
Bookmarked:33 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
19 votes for this article.
Popularity: 4.48 Rating: 3.50 out of 5
3 votes, 15.8%
1
1 vote, 5.3%
2
6 votes, 31.6%
3

4
9 votes, 47.4%
5

Screenshot 1

Figure 1: Colorized options

Screenshot 2

Figure 2: Enabled options

Introduction

Some time ago I was writing a Corporate System with lots of menu options, so it was not easy to visually keep track of implemented and pending options. As a former Visual C++ developer, I miss the MFC's main menu implementation, where non-implemented options appear disabled automatically.

I have not reproduced the exactly MFC functionality, but extended the idea to color signalling, as shown in figures above. This provides my customer a clear idea of work progress. Optionally, the tool tips will show the associated method name for each menu option.

Using the Code

For using the MenuDecorator class, you must follow two simple steps:

  1. Add MenuDecorator.cs class file to your project.
  2. Inside your code (maybe main form's constructor), call the ColorizeImplementedOptions() or EnableImplementedOptions() static method, passing a reference to your MenuStrip object.

The first method needs to be invoked passing two colors also for implemented and non-implemented options, as shown below, plus a boolean value to show methods' name in tooltips:

public partial class MainForm : Form
{
    // Main form constructor
    public MainForm()
    {
        InitializeComponent(); // this method is generated by Visual Studio IDE

        // Call static function, blue for implemented options, red for non implemented
        // and also show tooltips
        MenuDecorator.ColorizeImplementedOptions
                (this.MainMenu, Color.Blue, Color.Red, true);

        // etc...

This will produce a result similar to Figure 1.

The second static method, will not change option's colors, but will enable implemented options and disable others. Also you can specify to show the tool tips with associated method names. Here is an example of use:

public partial class MainForm : Form
{
    // Main form constructor
    public MainForm()
    {
        InitializeComponent(); // Method generated by Visual Studio IDE

        // Call static function, and show tooltips
        MenuDecorator.EnableImplementedOptions(this.MainMenu, true);

        // etc...

The result will be similar to Figure 2.

Points of Interest

The core section of this little static class is a complex sequence of reflection methods to establish if a specific menu option has some Click event attached to it. It is called recursively, traversing all menu trees. Here is the code portion where Click event is evaluated for enabling or disabling:

public class MenuDecorator
{
    private const BindingFlags Flags = BindingFlags.Static | BindingFlags.Instance |
        BindingFlags.NonPublic | BindingFlags.FlattenHierarchy;
    private static FieldInfo ClickInfo =
        typeof(ToolStripMenuItem).GetField("EventClick", Flags);

    // ...

    private static void EnableMenuItem(ToolStripMenuItem _item)
    {
        PropertyInfo events = _item.GetType().GetProperty("Events", Flags);

        if (_item.HasDropDownItems)
        {
            _item.Enabled = true; // Always enable options with children
            foreach (ToolStripItem dropitem in _item.DropDownItems)
            {
                // recursively search child options
                if (dropitem.GetType() == typeof(ToolStripMenuItem))
                    EnableMenuItem((ToolStripMenuItem)dropitem);
            }
        }
        else
        {
            // 'handlers' will be null if there are no events for this menu option
            EventHandlerList handlers =
                (EventHandlerList)events.GetValue(_item, null);
            Delegate d = handlers[ClickInfo.GetValue(_item)];
            if (_showTips)
                _item.ToolTipText = d == null ? "[empty]" : d.Method.Name;
            _item.Enabled = !object.Equals(d, null);
        }
    }

The solution file included with this article, has been produced with Visual Studio 2008, so you won't be able to load directly from Visual Studio 2005, but you can create a new solution and attach the project file manually.

History

  • 4th June, 2007 - First version
  • 29th November, 2007 - Added tooltip feature
  • 27th June, 2008 - General revision

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Jaime Olivares


Member


Computer Electronics professional and senior Windows C++ and C# developer with experience in many other programming languages, platforms and application areas including communications, simulation systems, GIS, graphics and mobile issues.
Also have experience in electronic interfaces development, specially for military applications.
Currently intensively working with Visual C# 2008.
Top-100 contributor at Experts-Exchange forum.
Occupation: Software Developer (Senior)
Company: Royal Philips Electronics - Healthcare Informatics
Location: Peru Peru

Other popular Menus articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 3 of 3 (Total in Forum: 3) (Refresh)FirstPrevNext
Generalvery Good Pinmemberhmdsajadi22:29 11 Nov '07  
GeneralRe: very Good PinmemberJaime Olivares5:42 12 Nov '07  
GeneralGood PinmemberHypothalamus4:59 5 Jun '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 30 Jun 2008
Editor: Deeksha Shenoy
Copyright 2007 by Jaime Olivares
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project