|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
Figure 1: Colorized options
Figure 2: Enabled options
IntroductionSome 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 CodeFor using the
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 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 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
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||