Csharp/C Sharp by API/System.Windows.Forms/MenuItem — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 12:09, 26 мая 2010

extends 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; }
     }
}


MenuItem.Checked

 
using System;
using System.Drawing;
using System.Windows.Forms;
   
class CheckAndRadioCheck: Form
{
     MenuItem miColor, miFill;
   
     public static void Main()
     {
          Application.Run(new CheckAndRadioCheck());
     }
     public CheckAndRadioCheck()
     {
          ResizeRedraw = true;
   
          string[]     astrColor = {"Black", "Blue",    "Green",  "Cyan",
                                    "Red",   "Magenta", "Yellow", "White"};
          MenuItem[]   ami       = new MenuItem[astrColor.Length + 2];
          EventHandler ehColor   = new EventHandler(MenuFormatColorOnClick);
   
          for (int i = 0; i < astrColor.Length; i++)
          {
               ami[i] = new MenuItem(astrColor[i], ehColor);
               ami[i].RadioCheck = true;
          }
          miColor = ami[0];
          miColor.Checked = true;
   
          ami[astrColor.Length] = new MenuItem("-");
          
          miFill = new MenuItem("&Fill",new EventHandler(MenuFormatFillOnClick));
   
          ami[astrColor.Length + 1] = miFill;
   
          MenuItem mi = new MenuItem("&Format", ami);
          
          Menu = new MainMenu(new MenuItem[] {mi});
     }
     void MenuFormatColorOnClick(object obj, EventArgs ea)
     {
          miColor.Checked = false;
          miColor = (MenuItem)obj;
          miColor.Checked = true;
   
          Invalidate();
     }
     void MenuFormatFillOnClick(object obj, EventArgs ea)
     {
          MenuItem mi = (MenuItem)obj;
   
          mi.Checked ^= true;
   
          Invalidate();
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          if (miFill.Checked)
          {
               Console.WriteLine("fill");
          }
          else
          {
               Console.WriteLine("not fill");
          }
     }
}


MenuItem.Click

  

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
class ImageClip : Form {
    MenuItem miCut, miCopy, miPaste, miDel;
    Image image;
    public static void Main() {
        Application.Run(new ImageClip());
    }
    public ImageClip() {
        Text = "Image Clip";
        MenuItem mi = new MenuItem("&Edit");
        mi.Popup += new EventHandler(MenuEditOnPopup);
        Menu.MenuItems.Add(mi);
        int index = Menu.MenuItems.Count - 1;
        miCut = new MenuItem("Cu&t");
        miCut.Click += new EventHandler(MenuEditCutOnClick);
        miCut.Shortcut = Shortcut.CtrlX;
        Menu.MenuItems[index].MenuItems.Add(miCut);
        miCopy = new MenuItem("&Copy");
        miCopy.Click += new EventHandler(MenuEditCopyOnClick);
        miCopy.Shortcut = Shortcut.CtrlC;
        Menu.MenuItems[index].MenuItems.Add(miCopy);
        miPaste = new MenuItem("&Paste");
        miPaste.Click += new EventHandler(MenuEditPasteOnClick);
        miPaste.Shortcut = Shortcut.CtrlV;
        Menu.MenuItems[index].MenuItems.Add(miPaste);
        miDel = new MenuItem("De&lete");
        miDel.Click += new EventHandler(MenuEditDelOnClick);
        miDel.Shortcut = Shortcut.Del;
        Menu.MenuItems[index].MenuItems.Add(miDel);
    }
    void MenuEditOnPopup(object obj, EventArgs ea) {
        miCut.Enabled = miCopy.Enabled = miDel.Enabled = image != null;
        IDataObject data = Clipboard.GetDataObject();
        miPaste.Enabled = data.GetDataPresent(typeof(Bitmap)) || data.GetDataPresent(typeof(Metafile));
    }
    void MenuEditCutOnClick(object obj, EventArgs ea) {
        MenuEditCopyOnClick(obj, ea);
        MenuEditDelOnClick(obj, ea);
    }
    void MenuEditCopyOnClick(object obj, EventArgs ea) {
        Clipboard.SetDataObject(image, true);
    }
    void MenuEditPasteOnClick(object obj, EventArgs ea) {
        IDataObject data = Clipboard.GetDataObject();
        if (data.GetDataPresent(typeof(Metafile)))
            image = (Image)data.GetData(typeof(Metafile));
        else if (data.GetDataPresent(typeof(Bitmap)))
            image = (Image)data.GetData(typeof(Bitmap));
        Invalidate();
    }
    void MenuEditDelOnClick(object obj, EventArgs ea) {
        image = null;
        Invalidate();
    }
}


MenuItem.DrawItem

 
using System;
using System.Drawing;
using System.Windows.Forms;
   
class HelpMenu: Form
{
     Bitmap bmHelp;
   
     public static void Main()
     {
          Application.Run(new HelpMenu());
     }
     public HelpMenu()
     {
          bmHelp = new Bitmap(GetType(), "help.bmp");
   
          Menu = new MainMenu();
          Menu.MenuItems.Add("&Help");
   
          MenuItem mi     = new MenuItem("&Help");
          mi.OwnerDraw    = true;
          mi.Click       += new EventHandler(MenuHelpOnClick);
          mi.DrawItem    += new DrawItemEventHandler(MenuHelpOnDrawItem);
          mi.MeasureItem += new MeasureItemEventHandler(MenuHelpOnMeasureItem);
   
          Menu.MenuItems[0].MenuItems.Add(mi);
     }
     void MenuHelpOnMeasureItem(object obj, MeasureItemEventArgs miea)
     {
          miea.ItemWidth  = bmHelp.Width;
          miea.ItemHeight = bmHelp.Height;
     }
     void MenuHelpOnDrawItem(object obj, DrawItemEventArgs diea)
     {
          Rectangle rect = diea.Bounds;
          rect.X += diea.Bounds.Width - bmHelp.Width;
          rect.Width = bmHelp.Width;
   
          diea.DrawBackground();
          diea.Graphics.DrawImage(bmHelp, rect);
     }
     void MenuHelpOnClick(object obj, EventArgs ea)
     {
          MessageBox.Show("Help", Text);
     }
}


MenuItem.MeasureItem

 
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class MenuItemOwnerDraw : System.Windows.Forms.Form
{
  private System.Windows.Forms.MainMenu mainMenu1;
  private System.Windows.Forms.MenuItem mnuNew;
  private System.Windows.Forms.MenuItem mnuFile;
  private System.Windows.Forms.MenuItem mnuFileClose;
  private System.Windows.Forms.MenuItem mnuFileSave;
  private System.Windows.Forms.MenuItem mnuFileSaveAs;
  private System.Windows.Forms.MenuItem mnuEdit;
  private System.Windows.Forms.MenuItem mnuEditCopy;
  private System.Windows.Forms.MenuItem mnuEditPaste;
  private System.Windows.Forms.MenuItem mnuOption1;
  private System.Windows.Forms.MenuItem mnuOption2;
  private System.Windows.Forms.MenuItem mnuOption3;
  private System.Windows.Forms.MenuItem mnuROption1;
  private System.Windows.Forms.MenuItem mnuROption2;
  private System.Windows.Forms.MenuItem mnuROption3;
  private System.Windows.Forms.MenuItem mnuWindow;
  private System.Windows.Forms.MenuItem mnuOptions;
  private System.Windows.Forms.MenuItem mnuRadioOptions;
  private System.Windows.Forms.MenuItem mnuMenu1;
  private System.Windows.Forms.MenuItem mnuMenu11;
  private System.Windows.Forms.MenuItem mnuMenu12;
  private System.Windows.Forms.MenuItem mnuMenu13;
  private System.Windows.Forms.MenuItem mnuMenu14;
  private System.Windows.Forms.MenuItem mnuMenu2;
  private System.Windows.Forms.MenuItem mnuMenu21;
  private System.Windows.Forms.MenuItem mnuMenu22;
  private System.Windows.Forms.MenuItem mnuMenu23;
  private System.Windows.Forms.MenuItem mnuMenu24;
  private System.Windows.Forms.MenuItem mnuMerge;
  private System.Windows.Forms.MenuItem mnuODShazam;
  private string[] files = { @"YourFile.bmp", @"YourFile.bmp" };
  private System.Windows.Forms.MenuItem mnuODVote;
  private System.Windows.Forms.MenuItem mnuSpecial;
  private System.ruponentModel.Container components = null;
  public MenuItemOwnerDraw()
  {
    InitializeComponent();
  }
  protected override void Dispose( bool disposing )
  {
    if( disposing )
    {
      if (components != null) 
      {
        components.Dispose();
      }
    }
    base.Dispose( disposing );
  }
  private void InitializeComponent()
  {
    this.mainMenu1 = new System.Windows.Forms.MainMenu();
    this.mnuFile = new System.Windows.Forms.MenuItem();
    this.mnuNew = new System.Windows.Forms.MenuItem();
    this.mnuFileClose = new System.Windows.Forms.MenuItem();
    this.mnuFileSave = new System.Windows.Forms.MenuItem();
    this.mnuFileSaveAs = new System.Windows.Forms.MenuItem();
    this.mnuEdit = new System.Windows.Forms.MenuItem();
    this.mnuEditCopy = new System.Windows.Forms.MenuItem();
    this.mnuEditPaste = new System.Windows.Forms.MenuItem();
    this.mnuOptions = new System.Windows.Forms.MenuItem();
    this.mnuOption1 = new System.Windows.Forms.MenuItem();
    this.mnuOption2 = new System.Windows.Forms.MenuItem();
    this.mnuOption3 = new System.Windows.Forms.MenuItem();
    this.mnuRadioOptions = new System.Windows.Forms.MenuItem();
    this.mnuROption1 = new System.Windows.Forms.MenuItem();
    this.mnuROption2 = new System.Windows.Forms.MenuItem();
    this.mnuROption3 = new System.Windows.Forms.MenuItem();
    this.mnuSpecial = new System.Windows.Forms.MenuItem();
    this.mnuODVote = new System.Windows.Forms.MenuItem();
    this.mnuODShazam = new System.Windows.Forms.MenuItem();
    this.mnuWindow = new System.Windows.Forms.MenuItem();
    this.mnuMenu1 = new System.Windows.Forms.MenuItem();
    this.mnuMenu11 = new System.Windows.Forms.MenuItem();
    this.mnuMenu12 = new System.Windows.Forms.MenuItem();
    this.mnuMenu13 = new System.Windows.Forms.MenuItem();
    this.mnuMenu14 = new System.Windows.Forms.MenuItem();
    this.mnuMerge = new System.Windows.Forms.MenuItem();
    this.mnuMenu2 = new System.Windows.Forms.MenuItem();
    this.mnuMenu21 = new System.Windows.Forms.MenuItem();
    this.mnuMenu22 = new System.Windows.Forms.MenuItem();
    this.mnuMenu23 = new System.Windows.Forms.MenuItem();
    this.mnuMenu24 = new System.Windows.Forms.MenuItem();
    // 
    // mainMenu1
    // 
    this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                          this.mnuFile,
                                          this.mnuEdit,
                                          this.mnuOptions,
                                          this.mnuRadioOptions,
                                          this.mnuSpecial,
                                          this.mnuWindow,
                                          this.mnuMenu1,
                                          this.mnuMenu2});
    // 
    // mnuFile
    // 
    this.mnuFile.Index = 0;
    this.mnuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                        this.mnuNew,
                                        this.mnuFileClose,
                                        this.mnuFileSave,
                                        this.mnuFileSaveAs});
    this.mnuFile.Text = "File";
    // 
    // mnuNew
    // 
    this.mnuNew.Index = 0;
    this.mnuNew.Shortcut = System.Windows.Forms.Shortcut.CtrlN;
    this.mnuNew.Text = "&New";
    this.mnuNew.Click += new System.EventHandler(this.mnuNew_Click);
    // 
    // mnuFileClose
    // 
    this.mnuFileClose.Index = 2;
    this.mnuFileClose.Text = "Close";
    // 
    // mnuFileSave
    // 
    this.mnuFileSave.Index = 3;
    this.mnuFileSave.Text = "Save";
    this.mnuFileSave.Click += new System.EventHandler(this.mnuFileSave_Click);
    // 
    // mnuFileSaveAs
    // 
    this.mnuFileSaveAs.Index = 4;
    this.mnuFileSaveAs.Text = "Save&As";
    this.mnuFileSaveAs.Click += new System.EventHandler(this.mnuFileSaveAs_Click);
    // 
    // mnuEdit
    // 
    this.mnuEdit.Index = 1;
    this.mnuEdit.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                        this.mnuEditCopy,
                                        this.mnuEditPaste});
    this.mnuEdit.Text = "Edit";
    // 
    // mnuEditCopy
    // 
    this.mnuEditCopy.Index = 0;
    this.mnuEditCopy.Text = "&Copy";
    this.mnuEditCopy.Click += new System.EventHandler(this.mnuEditCopy_Click);
    // 
    // mnuEditPaste
    // 
    this.mnuEditPaste.Index = 1;
    this.mnuEditPaste.Text = "Paste";
    this.mnuEditPaste.Click += new System.EventHandler(this.mnuEditPaste_Click);
    // 
    // mnuOptions
    // 
    this.mnuOptions.Index = 2;
    this.mnuOptions.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                           this.mnuOption1,
                                           this.mnuOption2,
                                           this.mnuOption3});
    this.mnuOptions.Text = "Options";
    // 
    // mnuOption1
    // 
    this.mnuOption1.Index = 0;
    this.mnuOption1.Text = "Option1";
    this.mnuOption1.Click += new System.EventHandler(this.Option_Click);
    // 
    // mnuOption2
    // 
    this.mnuOption2.Index = 1;
    this.mnuOption2.Text = "Option2";
    this.mnuOption2.Click += new System.EventHandler(this.Option_Click);
    // 
    // mnuOption3
    // 
    this.mnuOption3.Index = 2;
    this.mnuOption3.Text = "Option3";
    this.mnuOption3.Click += new System.EventHandler(this.Option_Click);
    // 
    // mnuRadioOptions
    // 
    this.mnuRadioOptions.Index = 3;
    this.mnuRadioOptions.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                            this.mnuROption1,
                                            this.mnuROption2,
                                            this.mnuROption3});
    this.mnuRadioOptions.Text = "Radio Options";
    // 
    // mnuROption1
    // 
    this.mnuROption1.Index = 0;
    this.mnuROption1.RadioCheck = true;
    this.mnuROption1.Text = "Radio Option 1";
    this.mnuROption1.Click += new System.EventHandler(this.RadioOption_Click);
    // 
    // mnuROption2
    // 
    this.mnuROption2.Index = 1;
    this.mnuROption2.RadioCheck = true;
    this.mnuROption2.Text = "Radio Option 2";
    this.mnuROption2.Click += new System.EventHandler(this.RadioOption_Click);
    // 
    // mnuROption3
    // 
    this.mnuROption3.Index = 2;
    this.mnuROption3.RadioCheck = true;
    this.mnuROption3.Text = "Radio Option 3";
    this.mnuROption3.Click += new System.EventHandler(this.RadioOption_Click);
    // 
    // mnuSpecial
    // 
    this.mnuSpecial.Index = 4;
    this.mnuSpecial.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                           this.mnuODVote,
                                           this.mnuODShazam});
    this.mnuSpecial.Text = "Special";
    // 
    // mnuODVote
    // 
    this.mnuODVote.Index = 0;
    this.mnuODVote.OwnerDraw = true;
    this.mnuODVote.Text = "Vote";
    this.mnuODVote.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.OnDrawItem);
    this.mnuODVote.Click += new System.EventHandler(this.mnuODDraw_Click);
    this.mnuODVote.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.OnMeasureItem);
    // 
    // mnuODShazam
    // 
    this.mnuODShazam.Index = 1;
    this.mnuODShazam.OwnerDraw = true;
    this.mnuODShazam.Text = "Shazam";
    this.mnuODShazam.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.OnDrawItem);
    this.mnuODShazam.Click += new System.EventHandler(this.mnuODDraw_Click);
    this.mnuODShazam.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.OnMeasureItem);
    // 
    // mnuWindow
    // 
    this.mnuWindow.Index = 5;
    this.mnuWindow.MdiList = true;
    this.mnuWindow.MergeOrder = 99;
    this.mnuWindow.Text = "&Window";
    // 
    // mnuMenu1
    // 
    this.mnuMenu1.Index = 6;
    this.mnuMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                         this.mnuMenu11,
                                         this.mnuMenu12,
                                         this.mnuMenu13,
                                         this.mnuMenu14,
                                         this.mnuMerge});
    this.mnuMenu1.Text = "Menu 1";
    // 
    // mnuMenu11
    // 
    this.mnuMenu11.Index = 0;
    this.mnuMenu11.MergeOrder = 1;
    this.mnuMenu11.Text = "Menu 1.1";
    // 
    // mnuMenu12
    // 
    this.mnuMenu12.Index = 1;
    this.mnuMenu12.MergeOrder = 2;
    this.mnuMenu12.Text = "Menu 1.2";
    // 
    // mnuMenu13
    // 
    this.mnuMenu13.Index = 2;
    this.mnuMenu13.MergeOrder = 3;
    this.mnuMenu13.Text = "Menu 1.3";
    // 
    // mnuMenu14
    // 
    this.mnuMenu14.Index = 3;
    this.mnuMenu14.MergeOrder = 4;
    this.mnuMenu14.Text = "Menu 1.4";
    // 
    // mnuMerge
    // 
    this.mnuMerge.Index = 4;
    this.mnuMerge.MergeOrder = 99;
    this.mnuMerge.Text = "Merge!";
    this.mnuMerge.Click += new System.EventHandler(this.mnuMerge_Click);
    // 
    // mnuMenu2
    // 
    this.mnuMenu2.Index = 7;
    this.mnuMenu2.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                         this.mnuMenu21,
                                         this.mnuMenu22,
                                         this.mnuMenu23,
                                         this.mnuMenu24});
    this.mnuMenu2.Text = "Menu 2";
    // 
    // mnuMenu21
    // 
    this.mnuMenu21.Index = 0;
    this.mnuMenu21.MergeOrder = 1;
    this.mnuMenu21.Text = "Menu 2.1";
    // 
    // mnuMenu22
    // 
    this.mnuMenu22.Index = 1;
    this.mnuMenu22.MergeOrder = 2;
    this.mnuMenu22.MergeType = System.Windows.Forms.MenuMerge.Replace;
    this.mnuMenu22.Text = "Menu 2.2";
    // 
    // mnuMenu23
    // 
    this.mnuMenu23.Index = 2;
    this.mnuMenu23.MergeOrder = 3;
    this.mnuMenu23.MergeType = System.Windows.Forms.MenuMerge.Remove;
    this.mnuMenu23.Text = "Menu 2.3";
    // 
    // mnuMenu24
    // 
    this.mnuMenu24.Index = 3;
    this.mnuMenu24.MergeOrder = 5;
    this.mnuMenu24.Text = "Menu 2.4";
    // 
    // MenuItemOwnerDraw
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(536, 148);
    this.IsMdiContainer = true;
    this.Menu = this.mainMenu1;
    this.Name = "MenuItemOwnerDraw";
    this.Text = "MenuItemOwnerDraw";
  }
  [STAThread]
  static void Main() 
  {
    Application.Run(new MenuItemOwnerDraw());
  }

  private void mnuNew_Click(object sender, System.EventArgs e)
  {
  }
  private void Option_Click(object sender, System.EventArgs e)
  {
  }
  private void RadioOption_Click(object sender, System.EventArgs e)
  {
  }
  private void mnuEditCopy_Click(object sender, System.EventArgs e)
  {
  }
  private void mnuEditPaste_Click(object sender, System.EventArgs e)
  {
  }
  private void mnuFileSave_Click(object sender, System.EventArgs e)
  {
  }
  private void mnuFileSaveAs_Click(object sender, System.EventArgs e)
  {
  }
  private void mnuMerge_Click(object sender, System.EventArgs e)
  {
  }
  private void OnDrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
  {
    Image img = Image.FromFile(files[e.Index]);
    Rectangle r = e.Bounds;
    Pen pen = new Pen(e.BackColor,2);
    r.Inflate(-6,-6);
    e.Graphics.DrawRectangle(pen,r);
    e.Graphics.DrawImage(img,r);
  }
  private void OnMeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e)
  {
    Image img = Image.FromFile(files[e.Index]);
    e.ItemHeight = img.Height;
    e.ItemWidth = img.Width;
  }
  private void mnuODDraw_Click(object sender, System.EventArgs e)
  {
    MenuItem item = sender as MenuItem;
    if ( item != null )
    {
      string choice = item.Text;
    
      MessageBox.Show ("You clicked " + choice, "Menu Event Tester", 
        MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
    }
  }
}


MenuItem.MenuItems

 
using System;
using System.Drawing;
using System.Windows.Forms;
   
class FontMenu: Form
{
     const int iPointSize = 24;
     string    strFacename;
   
     public static void Main()
     {
          Application.Run(new FontMenu());
     }
     public FontMenu()
     {
          strFacename = Font.Name;
   
          Menu = new MainMenu();
   
          MenuItem mi = new MenuItem("&Facename");
          mi.Popup += new EventHandler(MenuFacenameOnPopup);
          mi.MenuItems.Add(" ");   // Necessary for pop-up call
          Menu.MenuItems.Add(mi);
     }
     void MenuFacenameOnPopup(object obj, EventArgs ea)
     {
          MenuItem     miFacename = (MenuItem)obj;
          FontFamily[] aff        = FontFamily.Families;
          EventHandler ehClick    = new EventHandler(MenuFacenameOnClick);
          MenuItem[]   ami        = new MenuItem[aff.Length];
   
          for (int i = 0; i < aff.Length; i++)
          {
               ami[i] = new MenuItem(aff[i].Name);
               ami[i].Click += ehClick;
   
               if (aff[i].Name == strFacename)
                    ami[i].Checked = true;
          }
          miFacename.MenuItems.Clear();
          miFacename.MenuItems.AddRange(ami);
     }
     void MenuFacenameOnClick(object obj, EventArgs ea)
     {    
          MenuItem mi = (MenuItem)obj;
          strFacename = mi.Text;
          Invalidate();
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          Graphics grfx = pea.Graphics;
          Font     font = new Font(strFacename, iPointSize);
   
          StringFormat strfmt  = new StringFormat();
          grfx.DrawString("Sample Text", font, new SolidBrush(ForeColor), 
                          ClientRectangle, strfmt);
     }                        
}


MenuItem.OnSelect

 
using System;
using System.Drawing;
using System.Windows.Forms;
   
class MenuItemHelp: MenuItem
{
     StatusBarPanel sbpHelpPanel;
     string         strHelpText;
     public MenuItemHelp(string strText): base(strText)
     {
     }
     public StatusBarPanel HelpPanel
     {
          get { return sbpHelpPanel; }
          set { sbpHelpPanel = value; }
     }
     public string HelpText
     {
          get { return strHelpText; }
          set { strHelpText = value; }
     }
     protected override void OnSelect(EventArgs ea)
     {
          base.OnSelect(ea);
   
          if (HelpPanel != null)
               HelpPanel.Text = HelpText;
     }
}

class MenuHelpSubclass: Form
{
     StatusBarPanel sbpMenuHelp;
     string         strSavePanelText;
   
     public static void Main()
     {
          Application.Run(new MenuHelpSubclass());
     }
     public MenuHelpSubclass()
     {
          StatusBar sb = new StatusBar();
          sb.Parent = this;
          sb.ShowPanels = true;
   
          sbpMenuHelp = new StatusBarPanel();
          sbpMenuHelp.Text = "Ready";
          sbpMenuHelp.AutoSize = StatusBarPanelAutoSize.Spring;
   
          sb.Panels.Add(sbpMenuHelp);
   
          Menu = new MainMenu();
          
          MenuItemHelp mi = new MenuItemHelp("&File");
          mi.HelpPanel = sbpMenuHelp;
          mi.HelpText = "Commands for working with files";
          Menu.MenuItems.Add(mi);
   
          mi = new MenuItemHelp("&Open...");
          mi.HelpPanel = sbpMenuHelp;
          mi.HelpText = "Opens an existing document";
          Menu.MenuItems[0].MenuItems.Add(mi);
          
          mi = new MenuItemHelp("&Close");
          mi.HelpPanel = sbpMenuHelp;
          mi.HelpText = "Closes the current document";
          Menu.MenuItems[0].MenuItems.Add(mi);
   
          mi = new MenuItemHelp("&Save");
          mi.HelpPanel = sbpMenuHelp;
          mi.HelpText = "Saves the current document";
          Menu.MenuItems[0].MenuItems.Add(mi);
   
          mi = new MenuItemHelp("&Edit");
          mi.HelpPanel = sbpMenuHelp;
          mi.HelpText = "Commands for editing the document";
          Menu.MenuItems.Add(mi);
   
          mi = new MenuItemHelp("Cu&t");
          mi.HelpPanel = sbpMenuHelp;
          mi.HelpText = "Deletes the selection and " +
                        "copies it to the clipboard";
          Menu.MenuItems[1].MenuItems.Add(mi);
          
          mi = new MenuItemHelp("&Copy");
          mi.HelpPanel = sbpMenuHelp;
          mi.HelpText = "Copies the selection to the clipboard";
          Menu.MenuItems[1].MenuItems.Add(mi);
   
          mi = new MenuItemHelp("&Paste");
          mi.HelpPanel = sbpMenuHelp;
          mi.HelpText = "Replaces the current selection " +
                        "with the clipboard contents";
          Menu.MenuItems[1].MenuItems.Add(mi);
     }
     protected override void OnMenuStart(EventArgs ea)
     {
          strSavePanelText = sbpMenuHelp.Text;
     }
     protected override void OnMenuComplete(EventArgs ea)
     {
          sbpMenuHelp.Text = strSavePanelText;
     }
}


MenuItem.Parent

 
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class MenuItemEventOption : System.Windows.Forms.Form
{
  private System.Windows.Forms.MainMenu mainMenu1;
  private System.Windows.Forms.MenuItem mnuNew;
  private System.Windows.Forms.MenuItem mnuFile;
  private System.Windows.Forms.MenuItem mnuFileOpen;
  private System.Windows.Forms.MenuItem mnuFileClose;
  private System.Windows.Forms.MenuItem mnuFileSave;
  private System.Windows.Forms.MenuItem mnuFileSaveAs;
  private System.Windows.Forms.MenuItem mnuEdit;
  private System.Windows.Forms.MenuItem mnuEditCopy;
  private System.Windows.Forms.MenuItem mnuEditPaste;
  private System.Windows.Forms.MenuItem mnuOption1;
  private System.Windows.Forms.MenuItem mnuOption2;
  private System.Windows.Forms.MenuItem mnuOption3;
  private System.Windows.Forms.MenuItem mnuROption1;
  private System.Windows.Forms.MenuItem mnuROption2;
  private System.Windows.Forms.MenuItem mnuROption3;
  private System.Windows.Forms.MenuItem mnuWindow;
  private System.Windows.Forms.MenuItem mnuOptions;
  private System.Windows.Forms.MenuItem mnuRadioOptions;
  private System.ruponentModel.Container components = null;
  public MenuItemEventOption()
  {
    InitializeComponent();
  }
  protected override void Dispose( bool disposing )
  {
    if( disposing )
    {
      if (components != null) 
      {
        components.Dispose();
      }
    }
    base.Dispose( disposing );
  }
  private void InitializeComponent()
  {
    this.mainMenu1 = new System.Windows.Forms.MainMenu();
    this.mnuFile = new System.Windows.Forms.MenuItem();
    this.mnuNew = new System.Windows.Forms.MenuItem();
    this.mnuFileOpen = new System.Windows.Forms.MenuItem();
    this.mnuFileClose = new System.Windows.Forms.MenuItem();
    this.mnuFileSave = new System.Windows.Forms.MenuItem();
    this.mnuFileSaveAs = new System.Windows.Forms.MenuItem();
    this.mnuEdit = new System.Windows.Forms.MenuItem();
    this.mnuEditCopy = new System.Windows.Forms.MenuItem();
    this.mnuEditPaste = new System.Windows.Forms.MenuItem();
    this.mnuOptions = new System.Windows.Forms.MenuItem();
    this.mnuOption1 = new System.Windows.Forms.MenuItem();
    this.mnuOption2 = new System.Windows.Forms.MenuItem();
    this.mnuOption3 = new System.Windows.Forms.MenuItem();
    this.mnuRadioOptions = new System.Windows.Forms.MenuItem();
    this.mnuROption1 = new System.Windows.Forms.MenuItem();
    this.mnuROption2 = new System.Windows.Forms.MenuItem();
    this.mnuROption3 = new System.Windows.Forms.MenuItem();
    this.mnuWindow = new System.Windows.Forms.MenuItem();
    this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
      this.mnuFile, this.mnuEdit, this.mnuOptions, this.mnuRadioOptions, this.mnuWindow});
    this.mnuFile.Index = 0;
    this.mnuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                        this.mnuNew,
                                        this.mnuFileOpen,
                                        this.mnuFileClose,
                                        this.mnuFileSave,
                                        this.mnuFileSaveAs});
    this.mnuFile.Text = "File";
    this.mnuNew.Index = 0;
    this.mnuNew.Shortcut = System.Windows.Forms.Shortcut.CtrlN;
    this.mnuNew.Text = "&New";
    this.mnuNew.Click += new System.EventHandler(this.mnuNew_Click);
    this.mnuFileOpen.Index = 1;
    this.mnuFileOpen.Text = "Open";
    this.mnuFileOpen.Click += new System.EventHandler(this.mnuFileOpen_Click);
    // 
    // mnuFileClose
    // 
    this.mnuFileClose.Index = 2;
    this.mnuFileClose.Text = "Close";
    this.mnuFileClose.Click += new System.EventHandler(this.mnuFileClose_Click);
    // 
    // mnuFileSave
    // 
    this.mnuFileSave.Index = 3;
    this.mnuFileSave.Text = "Save";
    this.mnuFileSave.Click += new System.EventHandler(this.mnuFileSave_Click);
    // 
    // mnuFileSaveAs
    // 
    this.mnuFileSaveAs.Index = 4;
    this.mnuFileSaveAs.Text = "Save&As";
    this.mnuFileSaveAs.Click += new System.EventHandler(this.mnuFileSaveAs_Click);
    // 
    // mnuEdit
    // 
    this.mnuEdit.Index = 1;
    this.mnuEdit.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                        this.mnuEditCopy,
                                        this.mnuEditPaste});
    this.mnuEdit.Text = "Edit";
    // 
    // mnuEditCopy
    // 
    this.mnuEditCopy.Index = 0;
    this.mnuEditCopy.Text = "&Copy";
    this.mnuEditCopy.Click += new System.EventHandler(this.mnuEditCopy_Click);
    // 
    // mnuEditPaste
    // 
    this.mnuEditPaste.Index = 1;
    this.mnuEditPaste.Text = "Paste";
    this.mnuEditPaste.Click += new System.EventHandler(this.mnuEditPaste_Click);
    // 
    // mnuOptions
    // 
    this.mnuOptions.Index = 2;
    this.mnuOptions.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                           this.mnuOption1,
                                           this.mnuOption2,
                                           this.mnuOption3});
    this.mnuOptions.Text = "Options";
    // 
    // mnuOption1
    // 
    this.mnuOption1.Index = 0;
    this.mnuOption1.Text = "Option1";
    this.mnuOption1.Click += new System.EventHandler(this.Option_Click);
    // 
    // mnuOption2
    // 
    this.mnuOption2.Index = 1;
    this.mnuOption2.Text = "Option2";
    this.mnuOption2.Click += new System.EventHandler(this.Option_Click);
    // 
    // mnuOption3
    // 
    this.mnuOption3.Index = 2;
    this.mnuOption3.Text = "Option3";
    this.mnuOption3.Click += new System.EventHandler(this.Option_Click);
    // 
    // mnuRadioOptions
    // 
    this.mnuRadioOptions.Index = 3;
    this.mnuRadioOptions.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                            this.mnuROption1,
                                            this.mnuROption2,
                                            this.mnuROption3});
    this.mnuRadioOptions.Text = "Radio Options";
    // 
    // mnuROption1
    // 
    this.mnuROption1.Index = 0;
    this.mnuROption1.RadioCheck = true;
    this.mnuROption1.Text = "Radio Option 1";
    this.mnuROption1.Click += new System.EventHandler(this.RadioOption_Click);
    // 
    // mnuROption2
    // 
    this.mnuROption2.Index = 1;
    this.mnuROption2.RadioCheck = true;
    this.mnuROption2.Text = "Radio Option 2";
    this.mnuROption2.Click += new System.EventHandler(this.RadioOption_Click);
    // 
    // mnuROption3
    // 
    this.mnuROption3.Index = 2;
    this.mnuROption3.RadioCheck = true;
    this.mnuROption3.Text = "Radio Option 3";
    this.mnuROption3.Click += new System.EventHandler(this.RadioOption_Click);
    // 
    // mnuWindow
    // 
    this.mnuWindow.Index = 4;
    this.mnuWindow.MdiList = true;
    this.mnuWindow.Text = "&Window";
    // 
    // MenuItemEventOption
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(292, 186);
    this.IsMdiContainer = true;
    this.Menu = this.mainMenu1;
    this.Name = "MenuItemEventOption";
    this.Text = "MenuItemEventOption";
  }
  [STAThread]
  static void Main() 
  {
    Application.Run(new MenuItemEventOption());
  }

  private void mnuNew_Click(object sender, System.EventArgs e)
  {
  }
    private void mnuFileOpen_Click(object sender, System.EventArgs e)
  {
    MessageBox.Show ("You clicked File Open", "Menu Event Tester",   MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  }
  private void mnuFileClose_Click(object sender, System.EventArgs e)
  {
    MessageBox.Show ("You clicked File Close", "Menu Event Tester",  MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  }
  private void Option_Click(object sender, System.EventArgs e)
  {
    MenuItem item = sender as MenuItem;
    if ( item != null )
    {
      item.Checked = ! item.Checked;
    }
  }
  private void RadioOption_Click(object sender, System.EventArgs e)
  {
    MenuItem item = sender as MenuItem;
    Menu parent = item.Parent;
    if ( item != null )
    {
      foreach ( MenuItem mi in parent.MenuItems )
        mi.Checked = false;
      item.Checked = true;
    }
  }
  private void mnuEditCopy_Click(object sender, System.EventArgs e)
  {
    MessageBox.Show ("You clicked Edit Copy", "Menu Event Tester", 
      MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  
  }
  private void mnuEditPaste_Click(object sender, System.EventArgs e)
  {
    MessageBox.Show ("You clicked Edit Paste", "Menu Event Tester", 
      MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  
  }
  private void mnuFileSave_Click(object sender, System.EventArgs e)
  {
    MessageBox.Show ("You clicked Save", "Menu Event Tester", 
      MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  }
  private void mnuFileSaveAs_Click(object sender, System.EventArgs e)
  {
    MessageBox.Show ("You clicked SaveAs", "Menu Event Tester", 
      MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  
  }
}


MenuItem.Popup

  
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
   
class RichTextPaste: Form
{
     string   strPastedText = "";
     MenuItem miPastePlain, miPasteRTF, miPasteHTML, miPasteCSV;
   
     public static void Main()
     {
          Application.Run(new RichTextPaste());
     }
     public RichTextPaste()
     {
          ResizeRedraw = true;
   
          Menu = new MainMenu();
   
          MenuItem mi = new MenuItem("&Edit");
          mi.Popup += new EventHandler(MenuEditOnPopup);
          Menu.MenuItems.Add(mi);
   
          miPastePlain = new MenuItem("Paste &Plain Text");
          miPastePlain.Click += new EventHandler(MenuEditPastePlainOnClick);
          Menu.MenuItems[0].MenuItems.Add(miPastePlain);
   
          miPasteRTF = new MenuItem("Paste &Rich Text Format");
          miPasteRTF.Click += new EventHandler(MenuEditPasteRTFOnClick);
          Menu.MenuItems[0].MenuItems.Add(miPasteRTF);
   
          miPasteHTML = new MenuItem("Paste &HTML");
          miPasteHTML.Click += new EventHandler(MenuEditPasteHTMLOnClick);
          Menu.MenuItems[0].MenuItems.Add(miPasteHTML);
   
          miPasteCSV = new MenuItem("Paste &Comma-Separated Values");
          miPasteCSV.Click += new EventHandler(MenuEditPasteCSVOnClick);
          Menu.MenuItems[0].MenuItems.Add(miPasteCSV);
     }
     void MenuEditOnPopup(object obj, EventArgs ea)
     {
          miPastePlain.Enabled = Clipboard.GetDataObject().GetDataPresent(typeof(string));
          miPasteRTF.Enabled = Clipboard.GetDataObject().GetDataPresent(DataFormats.Rtf);
          miPasteHTML.Enabled = Clipboard.GetDataObject().GetDataPresent(DataFormats.Html);
          miPasteCSV.Enabled = Clipboard.GetDataObject().GetDataPresent(DataFormats.rumaSeparatedValue);
     }
     void MenuEditPastePlainOnClick(object obj, EventArgs ea)
     {
          IDataObject data = Clipboard.GetDataObject();
   
          if (data.GetDataPresent(typeof(string)))
          {
               strPastedText = (string) data.GetData(typeof(string));
               Invalidate();
          }
     }
     void MenuEditPasteRTFOnClick(object obj, EventArgs ea)
     {
          IDataObject data = Clipboard.GetDataObject();
   
          if (data.GetDataPresent(DataFormats.Rtf))
          {
               strPastedText = (string) data.GetData(DataFormats.Rtf);
               Invalidate();
          }
     }
     void MenuEditPasteHTMLOnClick(object obj, EventArgs ea)
     {
          IDataObject data = Clipboard.GetDataObject();
   
          if (data.GetDataPresent(DataFormats.Html))
          {
               strPastedText = (string) data.GetData(DataFormats.Html);
               Invalidate();
          }
     }
     void MenuEditPasteCSVOnClick(object obj, EventArgs ea)
     {
          IDataObject data = Clipboard.GetDataObject();
   
          if (data.GetDataPresent(DataFormats.rumaSeparatedValue))
          {
               MemoryStream memstr = (MemoryStream) data.GetData("Csv");
               StreamReader streamreader = new StreamReader(memstr);
               strPastedText = streamreader.ReadToEnd();
               Invalidate();
          }
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          pea.Graphics.DrawString(strPastedText, Font, new SolidBrush(ForeColor),
                          ClientRectangle);
     }
}


MenuItem.Shortcut

 
using System;
using System.Drawing;
using System.Windows.Forms;
   
class StandardMenu: Form
{
     MenuItem miFileOpen, miFileSave;
     MenuItem miEditCut, miEditCopy, miEditPaste;
   
     bool bDocumentPresent  = true;
     bool bNonNullSelection = true;
     bool bStuffInClipboard = false;
   
     public static void Main()
     {
          Application.Run(new StandardMenu());
     }
     public StandardMenu()
     {
          Menu = new MainMenu();
   
          MenuItem mi = new MenuItem("&File");
          mi.Popup += new EventHandler(MenuFileOnPopup);
          Menu.MenuItems.Add(mi);
          int index = Menu.MenuItems.Count - 1;
   
          miFileOpen = new MenuItem("&Open...");
          miFileOpen.Click += new EventHandler(MenuFileOpenOnClick);
          miFileOpen.Shortcut = Shortcut.CtrlO;
          Menu.MenuItems[index].MenuItems.Add(miFileOpen);
   
          miFileSave  = new MenuItem("&Save");
          miFileSave.Click += new EventHandler(MenuFileSaveOnClick);
          miFileSave.Shortcut = Shortcut.CtrlS;
          Menu.MenuItems[index].MenuItems.Add(miFileSave);
   
          mi = new MenuItem("-");
          Menu.MenuItems[index].MenuItems.Add(mi);
   
          mi = new MenuItem("E&xit");
          mi.Click += new EventHandler(MenuFileExitOnClick);
          Menu.MenuItems[index].MenuItems.Add(mi);
   
          mi = new MenuItem("&Edit");
          mi.Popup += new EventHandler(MenuEditOnPopup);
          Menu.MenuItems.Add(mi);
          index = Menu.MenuItems.Count - 1;
   
          miEditCut = new MenuItem("Cu&t");
          miEditCut.Click += new EventHandler(MenuEditCutOnClick);
          miEditCut.Shortcut = Shortcut.CtrlX;
          Menu.MenuItems[index].MenuItems.Add(miEditCut);
   
          miEditCopy = new MenuItem("&Copy");
          miEditCopy.Click += new EventHandler(MenuEditCopyOnClick);
          miEditCopy.Shortcut = Shortcut.CtrlC;
          Menu.MenuItems[index].MenuItems.Add(miEditCopy);
   
          miEditPaste = new MenuItem("&Paste");
          miEditPaste.Click += new EventHandler(MenuEditCopyOnClick);
          miEditPaste.Shortcut = Shortcut.CtrlV;
          Menu.MenuItems[index].MenuItems.Add(miEditPaste);
   
          mi = new MenuItem("&Help");
          Menu.MenuItems.Add(mi);
          index = Menu.MenuItems.Count - 1;
   
          mi = new MenuItem("&About StandardMenu...");
          mi.Click += new EventHandler(MenuHelpAboutOnClick);
          Menu.MenuItems[index].MenuItems.Add(mi);
     }
     void MenuFileOnPopup(object obj, EventArgs ea)
     {
          miFileSave.Enabled = bDocumentPresent;
     }
     void MenuEditOnPopup(object obj, EventArgs ea)
     {
          miEditCut.Enabled = bNonNullSelection;
          miEditCopy.Enabled = bNonNullSelection;
          miEditPaste.Enabled = bStuffInClipboard;
     }
     void MenuFileOpenOnClick(object obj, EventArgs ea)
     { 
          MessageBox.Show("File Open dialog box!", Text);
     }
     void MenuFileSaveOnClick(object obj, EventArgs ea)
     {
          MessageBox.Show("File Save dialog box!", Text);
     }
     void MenuFileExitOnClick(object obj, EventArgs ea)
     {
          Close();
     }
     void MenuEditCutOnClick(object obj, EventArgs ea)
     {
     }
     void MenuEditCopyOnClick(object obj, EventArgs ea)
     {
     }
     void MenuEditPasteOnClick(object obj, EventArgs ea)
     {
     }
     void MenuHelpAboutOnClick(object obj, EventArgs ea)
     {
          MessageBox.Show("StandardMenu ?", Text);
     }
}


MenuItem.Text

 
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class Form1 : System.Windows.Forms.Form {
    private System.Windows.Forms.MainMenu File = new System.Windows.Forms.MainMenu();
    private System.Windows.Forms.MenuItem menuItem1 = new System.Windows.Forms.MenuItem();
    private System.Windows.Forms.MenuItem menuItem2 = new System.Windows.Forms.MenuItem();
    private System.Windows.Forms.MenuItem menuItem3 = new System.Windows.Forms.MenuItem();
    private System.Windows.Forms.Button button1 = new System.Windows.Forms.Button();
    private System.Windows.Forms.Button button2 = new System.Windows.Forms.Button();
    private System.ruponentModel.Container components = null;
    private int nIndex = 0;
    public Form1() {
        this.SuspendLayout();
        this.File.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {this.menuItem1});
        this.menuItem1.Index = 0;
        this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuItem2, this.menuItem3});
        this.menuItem1.Text = "File";
        this.menuItem2.Index = 0;
        this.menuItem2.Text = "&Close";
        this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
        this.menuItem3.Index = 1;
        this.menuItem3.Text = "E&xit";
        this.button1.Location = new System.Drawing.Point(40, 208);
        this.button1.Name = "button1";
        this.button1.TabIndex = 0;
        this.button1.Text = "&Add";
        this.button1.Click += new System.EventHandler(this.button1_Click);
        this.button2.Location = new System.Drawing.Point(176, 208);
        this.button2.Name = "button2";
        this.button2.TabIndex = 1;
        this.button2.Text = "&Close";
        this.button2.Click += new System.EventHandler(this.button2_Click);
        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(292, 273);
        this.Controls.AddRange(new System.Windows.Forms.Control[] { this.button2, this.button1});
        this.Menu = this.File;
        this.ResumeLayout(false);
    }
    [STAThread]
    static void Main() {
        Application.Run(new Form1());
    }
    private void button2_Click(object sender, System.EventArgs e) {
        Close();
    }
    private void menuItemHandler(object sender, System.EventArgs e) {
        MessageBox.Show(this, "Menu Handler Called");
        MenuItem mi = (MenuItem)sender;
        MessageBox.Show(this, "Menu Item: " + mi.Text);
    }
    private void button1_Click(object sender, System.EventArgs e) {
        MenuItem mi = new MenuItem("File " + (nIndex + 1), new EventHandler(menuItemHandler));
        this.menuItem1.MenuItems.Add(mi);
        nIndex++;
    }
    private void menuItem2_Click(object sender, System.EventArgs e) {
        Close();
    }
}


new MenuItem

 
using System;
using System.Drawing;
using System.Windows.Forms;
   
class OldFashionedMenu: Form
{
     MainMenu mmMain, mmFile, mmEdit;
   
     public static void Main()
     {
          Application.Run(new OldFashionedMenu());
     }
     public OldFashionedMenu()
     {
          EventHandler eh = new EventHandler(MenuOnClick);
   
          mmMain = new MainMenu(new MenuItem[] 
          { 
               new MenuItem("MAIN:"),
               new MenuItem("&File", new EventHandler(MenuFileOnClick)),
               new MenuItem("&Edit", new EventHandler(MenuEditOnClick))
          });
   
          mmFile = new MainMenu(new MenuItem[]
          {
               new MenuItem("FILE:"),
               new MenuItem("&New", eh),
               new MenuItem("&Open...", eh),
               new MenuItem("&Save", eh),
               new MenuItem("Save &As...", eh),
               new MenuItem("(&Main)", new EventHandler(MenuMainOnClick))
          });
          
          mmEdit = new MainMenu(new MenuItem[]
          {
               new MenuItem("EDIT:"),
               new MenuItem("Cu&t", eh),
               new MenuItem("&Copy", eh),
               new MenuItem("&Paste", eh),
               new MenuItem("De&lete", eh),
               new MenuItem("(&Main)", new EventHandler(MenuMainOnClick))
          });
   
          Menu = mmMain;
     }
     void MenuMainOnClick(object obj, EventArgs ea)
     {
          Menu = mmMain;
     }
     void MenuFileOnClick(object obj, EventArgs ea)
     {
          Menu = mmFile;
     }
     void MenuEditOnClick(object obj, EventArgs ea)
     {
          Menu = mmEdit;
     }
     void MenuOnClick(object obj, EventArgs ea)
     {
          MessageBox.Show("Menu item clicked!", Text);
     }
}


new MenuItem("&Open...",new EventHandler(MenuFileOpenOnClick),Shortcut.CtrlO)

 
                                   
using System;
using System.Drawing;
using System.Windows.Forms;
   
class FirstMainMenu: Form
{
     public static void Main()
     {
          Application.Run(new FirstMainMenu());
     }
     public FirstMainMenu()
     {
          MenuItem miOpen = new MenuItem("&Open...",
                                   new EventHandler(MenuFileOpenOnClick),
                                   Shortcut.CtrlO);
   
          MenuItem miSave = new MenuItem("&Save",
                                   new EventHandler(MenuFileSaveOnClick),
                                   Shortcut.CtrlS);
   
          MenuItem miSaveAs = new MenuItem("Save &As...",
                                   new EventHandler(MenuFileSaveAsOnClick));
   
          MenuItem miDash = new MenuItem("-");
   
          MenuItem miExit = new MenuItem("E&xit",
                                   new EventHandler(MenuFileExitOnClick));
          MenuItem miFile = new MenuItem("&File",
                                   new MenuItem[] {miOpen, miSave, miSaveAs,
                                                   miDash, miExit });
          MenuItem miCut = new MenuItem("Cu&t",
                                   new EventHandler(MenuEditCutOnClick),
                                   Shortcut.CtrlX);
   
          MenuItem miCopy = new MenuItem("&Copy",
                                   new EventHandler(MenuEditCopyOnClick),
                                   Shortcut.CtrlC);
   
          MenuItem miPaste = new MenuItem("&Paste",
                                   new EventHandler(MenuEditPasteOnClick),
                                   Shortcut.CtrlV);
          MenuItem miEdit = new MenuItem("&Edit",
                                   new MenuItem[] {miCut, miCopy, miPaste});
   
          MenuItem miAbout = new MenuItem("&About FirstMainMenu...",
                                   new EventHandler(MenuHelpAboutOnClick));
          MenuItem miHelp = new MenuItem("&Help", 
                                   new MenuItem[] {miAbout});
   
          Menu = new MainMenu(new MenuItem[] {miFile, miEdit, miHelp});
     }
     void MenuFileOpenOnClick(object obj, EventArgs ea)
     {
          MessageBox.Show("File Open item clicked!", Text);
     }
     void MenuFileSaveOnClick(object obj, EventArgs ea)
     {
          MessageBox.Show("File Save item clicked!", Text);
     }
     void MenuFileSaveAsOnClick(object obj, EventArgs ea)
     {
          MessageBox.Show("File Save As item clicked!", Text);
     }
     void MenuFileExitOnClick(object obj, EventArgs ea)
     {
          Close();
     }
     void MenuEditCutOnClick(object obj, EventArgs ea)
     {
          MessageBox.Show("Edit Cut item clicked!", Text);
     }
     void MenuEditCopyOnClick(object obj, EventArgs ea)
     {
          MessageBox.Show("Edit Copy item clicked!", Text);
     }
     void MenuEditPasteOnClick(object obj, EventArgs ea)
     {
          MessageBox.Show("Edit Paste item clicked!", Text);
     }
     void MenuHelpAboutOnClick(object obj, EventArgs ea)
     {
          MessageBox.Show(Text + " ?");
     }
}