![]() |
Desktop Development »
Menus »
Menus and Toolbars
Intermediate
License: The Code Project Open License (CPOL)
MenuDecorator - Helper Class to Diagnose your Main Menu ContentsBy Jaime OlivaresA class to colorize and enable the implemented main menu options in one step |
C# 2.0, Windows, .NET 2.0VS2008, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
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.
For using the MenuDecorator class, you must follow two simple steps:
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.
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.
| You must Sign In to use this message board. | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
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 |