/*
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.Yes) Application.Exit();
}
}
|