Click here to Skip to main content
Licence CPOL
First Posted 4 Jun 2007
Views 28,785
Downloads 583
Bookmarked 36 times

MenuDecorator - Helper Class to Diagnose your Main Menu Contents

By | 30 Jun 2008 | Article
A class to colorize and enable the implemented main menu options in one step

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

Software Developer (Senior)
Freelance (jaimeolivares.com)
Peru Peru

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, 3D graphics and mobile platform.
Also have experience in electronic interfaces development, specially for military applications.
Currently intensively working with Visual C# 2010.
Top-100 contributor at Experts-Exchange forum.
Can be reached at jaimeolivares.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalvery Good Pinmemberhmdsajadi21:29 11 Nov '07  
Smile | :)
GeneralRe: very Good PinmemberJaime Olivares4:42 12 Nov '07  
GeneralGood PinmemberHypothalamus3:59 5 Jun '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120529.1 | Last Updated 30 Jun 2008
Article Copyright 2007 by Jaime Olivares
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid