Add MenuStrip and ToolStripMenuItem to Form : Menu « GUI Windows Forms « C# / CSharp Tutorial






using System;
using System.Windows.Forms;

namespace SimpleWFApp
{
  class Program
  {
    static void Main()
    {
      Application.Run(new MainWindow("My Window", 200, 300));
    }
  }
  class MainWindow : Form 
  {
      private MenuStrip mnuMainMenu = new MenuStrip();
      private ToolStripMenuItem mnuFile = new ToolStripMenuItem();
      private ToolStripMenuItem mnuFileExit = new ToolStripMenuItem();
    
    public MainWindow(string title, int height, int width)
    {
      Text = title;
      Width = width;
      Height = height;
      
      CenterToScreen();

      mnuFile.Text = "&File";
      mnuMainMenu.Items.Add(mnuFile);
        
      mnuFileExit.Text = "E&xit";
      mnuFile.DropDownItems.Add(mnuFileExit);
      mnuFileExit.Click += new System.EventHandler(this.mnuFileExit_Click);

      Controls.Add(this.mnuMainMenu);
      MainMenuStrip = this.mnuMainMenu;
    }
    private void mnuFileExit_Click(object sender, EventArgs e)
    {
      MessageBox.Show(sender.ToString());
      Application.Exit();
    }
  }
}








23.39.Menu
23.39.1.Add Menu to FormAdd Menu to Form
23.39.2.Create a Menu without using the IDECreate a Menu without using the IDE
23.39.3.Menu RightToLeftMenu RightToLeft
23.39.4.Add SubmenuAdd Submenu
23.39.5.Set Label text in menu actionSet Label text in menu action
23.39.6.Context Menu Demo
23.39.7.Context Menu Using Add
23.39.8.Add MenuStrip and ToolStripMenuItem to Form