JScript .NET, Part XI: Creating Windows Forms: Creating Form Menus - Doc JavaScript
JScript .NET, Part XI: Creating Windows Forms
Creating Form Menus
In this last demo, we show you how to add a menu bar to a window. Suppose we want to add the File
menu at the top menu bar, as you find in most applications. We want to include a single item in this File
menu, the Save
entry. We also want to have a shortcut key for this entry, which is CTRL-S
, as in most applications. We'll just pop up a message box to confirm the menu item selection.
We add this feature on top of what we showed in Page 6. Windows forms support two relevant classes: MainMenu
and MenuItem
. A menu item in Windows forms needs three variables:
|
We call these variables menuMain
, menuFile
, and menuSave
. We define them as follows:
var menuMain : System.Windows.Forms.MainMenu; var menuFile : System.Windows.Forms.MenuItem; var menuSave : System.Windows.Forms.MenuItem;
We initialize a menu system from the bottom up. We first initialize 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:
Next: source code of anchors.js
Produced by Yehuda Shiran and Tomer Shiran
All Rights Reserved. Legal Notices.
Created: August 26, 2002
Revised: August 26, 2002
URL: https://www.webreference.com/js/column117/7.html