Add a Main Menu : Menu « GUI Windows Form « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# Book
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » GUI Windows Form » MenuScreenshots 
Add a Main Menu
Add a Main Menu

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Add a Main Menu. 
 
using System; 
using System.Windows.Forms; 
 
public class MenuForm : Form 
  MainMenu MyMenu; 
 
  public MenuForm() { 
    Text = "Adding a Main Menu"
 
    // Create a main menu object. 
    MyMenu  = new MainMenu()
 
    // Add top-level menu items to the menu. 
    MenuItem m1 = new MenuItem("File")
    MyMenu.MenuItems.Add(m1)
 
    MenuItem m2 = new MenuItem("Tools")
    MyMenu.MenuItems.Add(m2)
 
    // Create File submenu 
    MenuItem subm1 = new MenuItem("Open")
    m1.MenuItems.Add(subm1)
 
    MenuItem subm2 = new MenuItem("Close")
    m1.MenuItems.Add(subm2)
 
    MenuItem subm3 = new MenuItem("Exit")
    m1.MenuItems.Add(subm3)
 
    // Create Tools submenu 
    MenuItem subm4 = new MenuItem("Coordinates")
    m2.MenuItems.Add(subm4)
 
    MenuItem subm5 = new MenuItem("Change Size")
    m2.MenuItems.Add(subm5)
 
    MenuItem subm6 = new MenuItem("Restore")
    m2.MenuItems.Add(subm6)
 
 
    // Add event handlers for the menu items. 
    subm1.Click += new EventHandler(MMOpenClick)
    subm2.Click += new EventHandler(MMCloseClick)
    subm3.Click += new EventHandler(MMExitClick)
    subm4.Click += new EventHandler(MMCoordClick)
    subm5.Click += new EventHandler(MMChangeClick)
    subm6.Click += new EventHandler(MMRestoreClick)
 
    // Assign the menu to the form. 
    Menu = MyMenu; 
  }   
 
  [STAThread
  public static void Main() { 
    MenuForm skel = new MenuForm()
 
    Application.Run(skel)
  
 
  // Handler for main menu Coordinates selection. 
  protected void MMCoordClick(object who, EventArgs e) { 
    // Create a string that contains the cooridinates. 
    string size = 
      String.Format("{0}: {1}, {2}\n{3}: {4}, {5} "
                    "Top, Left", Top, Left, 
                    "Bottom, Right", Bottom, Right)
 
    // Display a message box. 
    MessageBox.Show(size, "Window Coordinates"
                    MessageBoxButtons.OK)
  
 
  // Handler for main menu Change selection. 
  protected void MMChangeClick(object who, EventArgs e) { 
    Width = Height = 200
  
 
  // Handler for main menu Restore selection. 
  protected void MMRestoreClick(object who, EventArgs e) { 
    Width = Height = 300
  
 
  // Handler for main menu Open selection. 
  protected void MMOpenClick(object who, EventArgs e) { 
 
    MessageBox.Show("Inactive""Inactive"
                    MessageBoxButtons.OK)
  
 
  // Handler for main menu Open selection. 
  protected void MMCloseClick(object who, EventArgs e) { 
 
    MessageBox.Show("Inactive""Inactive"
                    MessageBoxButtons.OK)
  
 
  // Handler for main menu Exit selection. 
  protected void MMExitClick(object who, EventArgs e) { 
 
    DialogResult result = MessageBox.Show("Stop Program?"
                            "Terminate"
                             MessageBoxButtons.YesNo)
 
    if(result == DialogResult.YesApplication.Exit()
  
}


           
       
Related examples in the same category
1.Add context menu to a form windowAdd context menu to a form window
2.Build Menu and context sensitive menu on your ownBuild Menu and context sensitive menu on your own
3.Add shortcut key to a menu itemAdd shortcut key to a menu item
4.Form MenuForm Menu
5.Menu Text Provider HostMenu Text Provider Host
6.Dynamic Menu
java2s.com  |  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.