Subclass MenuItem : MenuItem « GUI Windows Form « C# / C Sharp






Subclass MenuItem

 

using System;
using System.Drawing;
using System.Windows.Forms;
   
class BetterContextMenu: Form
{
     MenuItemColor micColor;
   
     public static void Main()
     {
          Application.Run(new BetterContextMenu());
     }
     public BetterContextMenu()
     {
          Text = "Better Context Menu Demo";
   
          EventHandler eh = new EventHandler(MenuColorOnClick);
   
          MenuItemColor[] amic = 
          {
               new MenuItemColor(Color.Black,   "&Black",   eh),
               new MenuItemColor(Color.Blue,    "B&lue",    eh),
               new MenuItemColor(Color.Green,   "&Green",   eh),
               new MenuItemColor(Color.Cyan,    "&Cyan",    eh),
               new MenuItemColor(Color.Red,     "&Red",     eh),
               new MenuItemColor(Color.Magenta, "&Magenta", eh),
               new MenuItemColor(Color.Yellow,  "&Yellow",  eh),
               new MenuItemColor(Color.White,   "&White",   eh)
          };
   
          foreach (MenuItemColor mic in amic)
               mic.RadioCheck = true;
   
          micColor = amic[3];
          micColor.Checked = true;
          BackColor = micColor.Color;
   
          ContextMenu = new ContextMenu(amic);
     }
     void MenuColorOnClick(object obj, EventArgs ea)
     {
          micColor.Checked = false;
          micColor = (MenuItemColor) obj;
          micColor.Checked = true;
   
          BackColor = micColor.Color;
     }
}
class MenuItemColor: MenuItem
{
     Color clr;
   
     public MenuItemColor(Color clr, string str, EventHandler eh):
                                                            base(str, eh)
     {
          Color = clr;
     }
     public Color Color
     {
          get { return clr; }
          set { clr = value; }
     }
}

 








Related examples in the same category

1.new MenuItem
2.Creating a menu on the fly.
3.Set MenuItem ShortCut
4.Font Menu
5.RadioCheck MenuItem
6.Hatch Brush Menu
7.Owner Drawn Menu ControlOwner Drawn Menu Control
8.Owner Drawn MenuOwner Drawn Menu
9.Add Help text for MenuItem
10.Help Menu