Programming in BASIC
Generalized Input Routines
by Michael E. Hepner
Get that data! File GENINPUT.ARC on your START disk.
Generalized Input Routines will give your programs flexibility. You can set up an edit screen with multiple fields and a separate menu bar. You can scroll through fields with the arrows, mouse or Return key and can access menu options or Desk Accessories without losing any work.
Here's how to do it.
Up and Running
Copy the file GENINPUT.ARC and ARCX.TTP to a blank, formatted disk and un-ARC GENINPUT.ARC, following the Disk Instructions elsewhere in this issue. You can run the programs LISTING1.PRG and LISTING2.PRG from the Desktop, but you will probably want to look at the ASCII program listings inside the GFA BASIC interpreter. Run GFA and click on Merge, then select the filename.
Listing One (LISTING1.LST) is a simple demonstration; it has just enough code for the program to run. Listing Two (LISTING2.LST) uses the Generalized Input Routines to build an address and phone list file.
Generalized Input Routines let you set up two types of menus: standard GEM menu bars and option list screens. The latter type lists options on the screen and lets the user select them with the mouse or function keys. Data entry screens display any number of data fields and wait for the user to enter data. A typical program would have one or more option list screens with some or all of the options taking the user to corresponding data entry screens.
Each screen is defined by a set of Data statements. Look at Listing One for a description of the different values. You can limit input by specifying a data type for each field; for example, only numbers are valid for a field if the type is set to "NUM." You can create as many data types as you like using the Instr( ) function.
Changing Menus on the Fly
Each screen may have a different set of customized drop down menus, which are also built from Data statements. You must code an If statement in the Menu_Handler procedure for each of your menu selections. (Editor's note: You can use Menu Builder, also on your START disk, to generate these menus. See the article opposite this one to learn how.) Selecting a menu item will interrupt whatever your program is doing, so take care that variables you are using won't be changed by whatever subroutine is called by menu events.
|This sample address book program is on your START disk.
You can move around the screen using the arrow keys, Return
or Tab, or the mouse.
These routines run in medium or high resolution and they use a combination of Print At and Text commands because each command has features the other doesn't. If you are using them both in the same program, keep in mind that the command Print At(X,Y);"ABC" gives the same results as the command Text 8*X-8,8*Y-1,"ABC" in medium resolution.
When you look at the routines that read the mouse position, notice that the program subtracts 22 from the mouse's vertical position because the mouse can point all the way to the top of the screen, but the vertical position for the Print At and Text commands starts below the window title.
The program has several routines to process keyboard input. The Up and Down arrow keys move the cursor from field to field in the order the fields were defined in the Fld_setup procedure. The Insert key toggles the insert mode on and off. The Escape key or the Clear Home key sets the current field to spaces. The Undo key restores the initial value of the current field. The Help key displays the help message for the current field. The Tab, Return and Enter keys signal that the current field is complete. Function key F10 means the entire record (all the fields on the screen) is finished.
You can write your own routines to process the other function keys or Control and Alternate key combinations. Since each key is processed separately, you have the flexibility to make it do whatever you want. In fact, customizing any aspect of Generalized Input Routines is easy; for an example, note how Listing Two performs additional validations on certain fields when you press Tab, Return or Enter.
An Address Book Sampler
The sample program in Listing Two uses the Generalized Input Routines to build an address file. You can create and edit a maximum of 90 records, each containing a name, street address, city, state, ZIP code and telephone number. LISTING2 also lets you list fifteen names, addresses and phone numbers in a column; the routines to do this are modified versions of the Generalized Input Routines.
Run LISTING2. You can move from one field to another at any time by using the arrow keys or the mouse. You can select the Load File and Save File options from the main drop-down menu. Notice that you can leave in the middle of a field to use the GEM desk accessories and come back to finish the field. When the record is done, press the F10 key. If all fields are valid the record is stored.
Michael E. Hepner supervises a group of computer systems analysts and has worked with computers for the last 18 years.