Classic Computer Magazine Archive COMPUTE! ISSUE 95 / APRIL 1988 / PAGE 82

Automatic Menus For IBM PC

Charles L. Banks

You might think that the easy-to-use, pop-up menus found in commercial software are too difficult to create and incorporate in your own programs, but with "Automatic Menus," they're a snap. BASICA is required for the PC, GW-BASIC for compatibles, or Cartridge BASIC for the PCjr.

A neat, user-friendly menu adds a professional touch to any piece of software, but writing and debugging menus for each new program can be both tedious and time consuming. And menus can use a lot of your computer's memory. "Automatic Menus" is a subroutine that you can include in any of your BASIC programs that will solve your menu problems and will give your programs a professional shine.

Getting Started

Since the Automatic Menus Demo is written entirely in BASIC, simply type it in, save a copy to disk, and type RUN.

In the demonstration program, lines 40–80 show how to use the menu subroutine which begins in line 1000. The Demo displays a 14-item menu, but any number up to 22 is possible. The RESTORE statement resets the DATA pointer and makes the menu reusable.

To call the Automatic Menus subroutines, first set M equal to the number of items in your menu (again, 22 is the maximum). Then load the ITEM$ array with your menu choices. The demonstration program shows an easy and efficient way to do this. Now, a GOSUB to the Automatic Menus subroutine in line 1000 instantly displays a simple, attractive menu in a box in the center of the screen.

The user selects an item by moving the up- and down-cursor keys and pressing Enter to activate the choice. Automatic Menus stores the selection in the variable SEL and returns to your main program.

Arranging The Array

Two statements are needed at the beginning of your main program for Automatic Menus to work as it's written. DIM ITEM$(22) creates the array for your list of menu items. OPTION BASE 1 causes all arrays to start with an index of 1 instead of 0. Having array indexes begin with 1 makes it much easier to keep track of selections and to use an ON SEL GOSUB command to process the user's menu selection. Remember, this affects all arrays in your program and must appear before any DIM statements.

Automatic Menus is written for 80-column text mode and it will work with any monitor—color or monochrome—though the COLOR statements may need to be modified for some displays. The program will also work in 40-column mode, but the value 40 in line 1050 must be changed to 20—the center of a 40-column screen. You may want to experiment with various colors to find which ones work best with each program.

How It Works

When Automatic Menus is entered, it first decides on which line the top menu item should be printed to center it vertically. It then calculates the length of the longest menu item and uses that value to center the menu horizontally. Next, a double-line box is drawn one character wider than the text. Finally, the menu items are printed in the box.

At this point, the program enters a loop to move through the menu to get the user's selection. First, the current selection is printed in reverse video. Then, any leftover keystrokes are cleared from the keyboard buffer, and an INKEY$ statement is used to get the next keystroke.

When a key is pressed, the current selection is reprinted in normal video. If the key was the down cursor, SEL is incremented by 1 or is wrapped back to the top of the menu. If the key was the up cursor, SEL is decremented by 1 or is wrapped to the bottom of the menu. The IF statements check for both the cursor keys and the numbers 2 and 8. This way, the routine works with or without the NUMLOCK key depressed.

When the Enter key is pressed, the screen clears and Automatic Menus returns to the main program with the user's menu selection stored in SEL.

Automatic Menus Demo

For instructions on entering this program, please refer to "COMPUTE!'s Guide to Typing In Programs" elsewhere in this issue.