Creating a App using JScript .NET and Windows Forms
Working with Menus
Most modern applications implement a system of menus and shortcut keys that make it easier for novice and advanced users to take advantage of the application's functionality. Menus make an application's functionality apparent to novice users, while shortcut keys appeal to more advanced users that prefer not to use a mouse for common tasks or frequently used application functionality.
The sample application features two types of menus: a main menu (a 'traditional' menu that runs across, just under the application's title bar) and a context menu (a menu that pops up when a user right-clicks on the form), as shown in figures 5 and 6.
Figure 6 - Context Menu
Whether you're working on creating a context menu or a main menu, the tasks you perform are the same - the only thing that changes are the names of the objects. The following listing shows how code the application's main menu (under the application's title bar):
private var mnuMain : System.Windows.Forms.MainMenu; private var mnuHelp : System.Windows.Forms.MenuItem; private var mnuHelpAbout : System.Windows.Forms.MenuItem; // mnuHelpAbout mnuHelpAbout = new System.Windows.Forms.MenuItem mnuHelpAbout.add_Click(mnuHelpAbout_Click) mnuHelpAbout.Text = "&About" mnuHelpAbout.Shortcut = Shortcut.CtrlA; mnuHelpAbout.ShowShortcut = true; // mnuHelp mnuHelp = new System.Windows.Forms.MenuItem mnuHelp.MenuItems.Add(mnuHelpAbout) mnuHelp.Text = "&Help" mnuHelp.ShowShortcut = false; // mnuMainMenu mnuMain = new System.Windows.Forms.MainMenu mnuMain.MenuItems.Add(mnuHelp) // *** Assign the main menu to the form... this.Menu = mnuMain;
The listing creates three objects: a MainMenu, and two MenuItem objects - configure the MenuItem objects before you add them to the MainMenu object, as shown in the listing.
MenuItem objects have a number of properties that make it easy to configure application-wide shortcut keys for an option, in addition to making keyboard based navigation possible. For example, you can access the Help menu by pressing Alt+H, and then A; alternately, you can also directly invoke the About menu by pressing Ctrl-A. See the listing for details on how to configure the menu and the shortcut keys.
Resizing Controls when the Form's Size Changes
A trait that most advanced graphical applications have a professional finish that comes from a very basic feature of Windows applications - the ability to resize a Window. When you resize a Window, like Internet Explorer's window, there are some controls that resize to match the proportions of the new window (instead of having a fixed size or staying in place as you resize the window).
Resizing controls based on the size of a containing window was a problem in the past, which often required a lot of extra code. The problem is so pervasive the Windows Forms includes automatic support for resizing controls through control anchors. For example, if you start the sample application and drag it's bottom right corner out and down, you'll see the text box, progress bar, and their associated buttons move along with the form's border as you resize it.
You can anchor a control using its Anchor property, which can have any or all values in the AnchorStyles enumeration. For example, the following line anchors the ProgressBar to the bottom, left and right of the form:
progressBar.Anchor = AnchorStyles.Bottom | AnchorStyles.Right | AnchorStyles.Left;
When you resize the form, the ProgressBar stays at the bottom of the form, and resizes along with changes in the forms left and right borders. The button beside the ProgressBar does not change size but moves with the form's right side border because it's anchored to the bottom right of the form, as shown:
btnProgressControl.Anchor = AnchorStyles.Bottom | AnchorStyles.Right ;
If you anchor the button to the Left as well, it ends up resizing along with the form, which is an unusual visual behavior for a button control. As a result, the button's anchor has it move only with the bottom and right borders such that it maintains the same relative position as the form resizes.
Working with the sample code
The sample code works with .NET version 1.0 only. Download the ZIP file, extract its contents into a new folder, and build the sample using the build command at a command prompt. You can run the application by typing controls at the command prompt while you're in the same directory that you build the application with.
The Windows Forms package offers a cohesive programming model that makes it easy to build forms that have rich capabilities and a high degree of interactivity. Most classes you'll require to build Windows Forms applications reside in the System.Windows.Forms and System.Drawing namespaces. Windows Forms controls can be customized by programmatically manipulating their properties and methods. The next article in this series goes through the process of creating a Web Service and consuming it from a Windows Forms application, ASP.NET application, and directly from Internet Explorer.
Essam Ahmed is the author of "< a href="http://books.internet.com/books/0764548689" target="new">JScript .NET Programming" (ISBN 0764548689, Published by Hungry Minds September 2001), many articles (including some at CodeGuru.com) and book reviews (also available at CodeGuru.com). Contact Essam at email@example.com, or at his Web site
# # #
Page 4 of 4