Creating Menus

 <  Day Day Up  >  

Creating Menus

Menus are used to provide events for displaying screens, setting options, and informing the user as to what's available. A good menu design makes an application more understandable and can actually determine how many technical support calls you get. The menu designers in FoxPro and in Visual Basic .NET ostensibly do the same thing, but what a difference!

The FoxPro Menu

In Visual FoxPro, we use the Menu Designer to build menus, whether Main or Context. The Menu Designer is, to be charitable, idiosyncratic. When you type MODIFY MENU name , you first choose between a menu and a shortcut (a context menu). The first level of the resulting designer creates menu pads, which are displayed horizontally at the top of the screen.

There are four possible types of menu entry: commands, procedures, submenus, and bars. Bars come from a fixed list of possible selections, and specific bar types (for example, Cut, Copy, and Paste) invoke the expected behaviors. Commands are one-line entries, whereas procedures can be multiple lines and result in little generated procedures in the MPR file (the auto-generated menu source code file) with system-supplied names that are the devil to find if you ever need to debug your menu. Submenus consist of menu bars, which will be displayed vertically. When you enter a submenu, visual cues as to their relation to the rest of the menu are minimal.

The design is saved in a table with the extension .mnx ( .mnt for the memo fields). When you're done, you must generate the source code for the menu by selecting the Generate option from the IDE menu. The resulting code is stored in a text file with the extension .mpr . Therefore, in your MAIN program code, you include the command

 

 DO MENU.MPR 

The resulting menu, named _MSYSMENU , is installed in place of the FoxPro IDE menu.

Menus can be added and removed as needed when the context changes. When you build the menu, you can select View, General Options from the FoxPro IDE menu and use options in the resulting dialog to specify that the menu is to be installed to the left or right of any pad in the current menu or to the right of the current menu. You can use PUSH MENU _MSYSMENU before adding a context menu, add it using DO MENU OtherMenu.MPR , and then use POP MENU _MSYSMENU when done to restore the menu as it was before. There are also shortcut menus, which can be popped up as needed.

Finally, individual menu items can be assigned hotkeys, which allow selections to be invoked without dropping down the menu pop-ups. Pads can also be marked as "selected" with a check or other character, and they can be grayed out to indicate that they are disabled.

All in all, the FoxPro menu is quite functional, but it's a little hard to get used to, and I wouldn't call it intuitive.

The Visual Basic .NET MainMenu Control

The Visual Basic .NET menu is a thing of beauty. It's intuitive, easy to understand, and easy to use. Unlike FoxPro's Menu Designer approach, the MainMenu control is selected from the Windows Form controls toolbar and dropped on the MainForm page. When selected, it appears in the upper-left corner of the screen. You can type directly into the pads and bars. Moving to the right or left of the last available selection produces a new empty box into which you can type the appropriate prompt. Selections can be moved around.

Changes to the menu control produce code in the form itself that is directly viewable ”there is no delayed code generation. To make the code more readable, you'll want to right-click on the menu control, turn on the Edit Names option, and type names for each menu selection. For example, I call the File pad mnuFile , the File, Exit selection below it mnuFileExit , and so forth. It makes it a lot easier to read the code, and it's easy.

To add code for any menu pad or bar, double-click on its name or caption and the corresponding code is displayed in the code window. Visual Basic .NET uses Handles clauses to connect the code you write to a particular menu component. The code is actually quite intuitive.

In a FoxPro menu selection to display a form, you'd probably use a DO FORM (formname) command. In a Visual Basic .NET menu pad to display a form based on your CustomerForm class (all forms in Visual Basic .NET are saved as class files with the .vb extension), you need to instantiate the form as an object and call the object's Show method, thusly:

 

 Dim oFrm as New CustomerForm oFrm.Show() 

Similarly you can instantiate and call methods on any other class. The Visual Basic .NET MainMenu control is absolutely one of the best things about the Visual Basic .NET Screen Designer. During a design review, I once irritated the FoxPro design team by alluding to the provenance of this lovely control, which came straight from Delphi (along with the entire Borland programming team).

Traversing the Controls in a Form

At some point, you're going to want to iterate through the controls in a form to do something with them ”enable or disable all of them, or perhaps automate binding. In FoxPro, it's done as shown in Listing 8.1.

Listing 8.1. Disabling All Controls Generically in Code in FoxPro
 FOR EACH Ctrl in THISFORM.Controls     Ctrl.Enabled = .F. ENDFOR 

In Visual Basic .NET, it's almost identical, as shown in Listing 8.2.

Listing 8.2. Disabling All Controls Generically in Code in Visual Basic .NET
 Dim Ctrl As Control For Each Ctrl in Controls     Ctrl.Enabled = False Next 

 <  Day Day Up  >  


Visual Fox Pro to Visual Basic.NET
Visual FoxPro to Visual Basic .NET
ISBN: 0672326493
EAN: 2147483647
Year: 2004
Pages: 130
Authors: Les Pinter

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net