September 10, 2002 - Implementing Windows Form's Menus
![]() |
September 10, 2002 Implementing Windows Form's Menus Tips: September 2002
Yehuda Shiran, Ph.D.
|
menuSave, then menuFile, and finally menuMain. Here is the definition of menuSave:
menuSave = new System.Windows.Forms.MenuItem();
menuSave.add_Click(menuSave_Clicked);
menuSave.Text = "Save";
menuSave.ShowShortcut = true;
menuSave.Shortcut = "CtrlS";
The event handler of a Click event is menuSave_Clicked. We just pop up a message box as a response to the Click event:
private function menuSave_Clicked(o : Object, e : EventArgs) {
MessageBox.Show("We should save the file now");
}
We continue with the initialization of menuFile. We can now add the previously-initialized menuSave:
menuFile = new System.Windows.Forms.MenuItem();
menuFile.MenuItems.Add(menuSave);
menuFile.Text = "File";
menuFile.ShowShortcut = false;
We finish the menu creation by initializing the top menu bar and adding menuFile to it:
menuMain = new System.Windows.Forms.MainMenu();
menuMain.MenuItems.Add(menuFile);
The package name is MenuPkg and the class name is MenuCls. We pop up the windows form by calling Application.Run():
Application.Run(new MenuPkg.MenuCls());
You should get the following window:




