PART II
C h a p t e r 2 6 :
U s e S y s t e m . W i n d o w s . F o r m s
865
// Create File submenu.
ToolStripMenuItem item1 = new ToolStripMenuItem("Open");
m1.DropDownItems.Add(item1);
ToolStripMenuItem item2 = new ToolStripMenuItem("Close");
m1.DropDownItems.Add(item2);
ToolStripMenuItem item3 = new ToolStripMenuItem("Exit");
m1.DropDownItems.Add(item3);
// Create Tools submenu.
ToolStripMenuItem item4 = new ToolStripMenuItem("Coordinates");
m2.DropDownItems.Add(item4);
ToolStripMenuItem item5 = new ToolStripMenuItem("Change Size");
m2.DropDownItems.Add(item5);
ToolStripMenuItem item6 = new ToolStripMenuItem("Restore");
m2.DropDownItems.Add(item6);
// Add event handlers for the menu items.
item1.Click += MMOpenClick;
item2.Click += MMCloseClick;
item3.Click += MMExitClick;
item4.Click += MMCoordClick;
item5.Click += MMChangeClick;
item6.Click += MMRestoreClick;
// Add to list of controls.
Controls.Add(MyMenu);
// Assign the menu to the form.
MainMenuStrip = MyMenu;
}
[STAThread]
static void Main() {
MenuForm skel = new MenuForm();
Application.EnableVisualStyles();
Application.Run(skel);
}
// Handler for main menu Coordinates selection.
void MMCoordClick(object who, EventArgs e) {
// Create a string that contains the coordinates.
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",
www.freepdf-books.com
866
P a r t I I :
E x p l o r i n g t h e C # L i b r a r y
MessageBoxButtons.OK);
}
// Handler for main menu Change selection.
void MMChangeClick(object who, EventArgs e) {
Width = Height = 200;
}
// Handler for main menu Restore selection.
void MMRestoreClick(object who, EventArgs e) {
Width = Height = 300;
}
// Handler for main menu Open selection.
void MMOpenClick(object who, EventArgs e) {
MessageBox.Show("Inactive", "Inactive",
MessageBoxButtons.OK);
}
// Handler for main menu Close selection.
void MMCloseClick(object who, EventArgs e) {
MessageBox.Show("Inactive", "Inactive",
MessageBoxButtons.OK);
}
// Handler for main menu Exit selection.
void MMExitClick(object who, EventArgs e) {
DialogResult result = MessageBox.Show("Stop Program?",
"Terminate",
MessageBoxButtons.YesNo);
if(result == DialogResult.Yes) Application.Exit();
}
}
F
IGURE
26-5
Sample output
from the
MenuStrip
program
www.freepdf-books.com
A
Documentation Comment
Quick Reference
C
# supports three types of comments. The first two are
Do'stlaringiz bilan baham: |