Microsoft KB Archive/40713

From BetaArchive Wiki
Knowledge Base

Macintosh BASIC Example to TAB between Multiple EDIT FIELDs

Article ID: 40713

Article Last Modified on 1/9/2003


  • Microsoft QuickBasic Compiler for Macintosh 1.0

This article was previously published under Q40713


This article gives an example of how to design a program to use multiple EDIT FIELDs and a BUTTON. Programs that use EDIT FIELDs or BUTTONS must use special event-handling techniques described below.

The sample program below is designed to detect the TAB key or a click of the mouse in order to move between the multiple EDIT FIELDs. You can type and edit text in any activated EDIT FIELD. When a BUTTON is clicked or you press ENTER or RETURN, the program prints the contents of each EDIT FIELD and stops.


To make a program that uses EDIT FIELD statements, you must use one of the following two techniques:

  1. Event trapping, using the ON DIALOG GOSUB label and DIALOG ON statements and a separate idle loop, which is interrupted by event GOSUBs
  2. Event polling, involving polling the value of x in a loop, where you invoke x=DIALOG(0) once per loop cycle and test x for various conditions (events)

The code example below uses the second technique, event polling, to handle TAB, RETURN, and mouse-clicking events that occur when using EDIT FIELDs.

The DIALOG(0) function returns a 7 if a TAB key was pressed in an EDIT FIELD. Once you know the TAB key was pressed in an EDIT FIELD, you can activate the next desired EDIT FIELD by executing the EDIT FIELD n statement. The DIALOG(2) function is useful to keep track of the most recently selected EDIT FIELD, so you can decide which EDIT FIELD to activate next with the EDIT FIELD n statement.

For more information about event trapping and polling, please refer to the following:

  1. Pages 115-119 of "QuickBASIC for Macintosh: Language Reference."
  2. A simple example of using one EDIT FIELD is shown on Page 113 of "QuickBASIC for Macintosh: Language Reference."
  3. "Microsoft Macinations: An Introduction to Microsoft BASIC for the Apple Macintosh" by Mitchell Waite (published by Microsoft Press 1985). Chapters 11 through 14 give the best tutorial available for using EDIT FIELDs and event handling. Every owner of BASIC or QuickBASIC for the Macintosh should have this book.

The example shown below expands upon the similar example on Page 132 of the "Microsoft BASIC Interpreter for Apple Macintosh" manual for Versions 2.00, 2.10, and 3.00.

Code Example

NumFields = 3  ' NumFields = Number of EDIT FIELDs defined below.
WINDOW 2, "HIT TAB", (50, 110) -(430, 240)
EDIT FIELD 3, "FIELD 3", (5, 82) - (250, 115)
EDIT FIELD 2, "FIELD 2", (5, 47) - (250, 80)
EDIT FIELD 1, "FIELD 1", (5, 10) - (250, 45)
BUTTON 1, 1, "Done", (290, 49) - (340, 67)
CurrentField = 1
EDIT FIELD CurrentField  ' Puts cursor at first EDIT FIELD.
     dial = DIALOG(0)  ' Invoke DIALOG(0) just once per loop cycle.
     IF dial = 1 THEN done  ' If button clicked, go to done.
     IF dial = 2 THEN
          CurrentField = DIALOG(2) ' Returns # of clicked EDIT FIELD.
          EDIT FIELD CurrentField  ' Activate clicked EDIT FIELD.
     END IF
     IF dial = 6 THEN done  ' If RETURN key pressed, go to done.
     IF dial = 7 THEN   ' If TAB pressed, activate next EDIT FIELD:
          CurrentField = (CurrentField MOD NumFields) + 1
          EDIT FIELD CurrentField
     END IF
done:   ' Use EDIT$(n) function to return contents of EDIT FIELD n:
     e1$ = EDIT$(1)
     e2$ = EDIT$(2)
     e3$ = EDIT$(3)
     EDIT FIELD CLOSE 0   ' Closes all edit fields.
     PRINT "EDIT FIELD 1 contents:" : PRINT e1$
     PRINT "EDIT FIELD 2 contents:" : PRINT e2$
     PRINT "EDIT FIELD 3 contents:" : PRINT e3$

Additional query words: BasicCom MQuickB

Keywords: KB40713