A simple, yet powerful, command you may be encountering for the first time with Just Basic is the WAIT statement. It requires no parameters; it simply instructs the program to WAIT.
You'll need to use the WAIT command in many places, the first of which will be the [quit] routine you create for any program.
INPUT "Miles driven? "; miles
INPUT "Fuel added? "; fuel
PRINT "Your fuel consumption equals "; (miles/fuel); " miles per gallon."
'WAIT
[quit]
CLS
END
Copy and paste the above code to your IDE and press run. Enter 100 miles driven and 10 gallons of fuel added. Poof! Zip! Nothing! Your program executed in the blink of an eye, cleared the screen and ended before you saw anything other than a brief flicker.
Now, remove the apostrophe (') before the WAIT statement and run again. Ahhh, that's what we wanted! We don't often use the WAIT statement for programs using the MAINWIN, but it is essential to use WAIT when we create a GUI.
'DEMO using WAIT statement
NOMAINWIN
WindowWidth=200
WindowHeight=180
UpperLeftX=INT((DisplayWidth-WindowWidth)/2)
UpperLeftY=INT((DisplayHeight-WindowHeight)/2)
BUTTON #1.btn1, "Calc", [calc], UL, 35, 130, 50, 20
BUTTON #1.btn2, "Exit", [quit], UL, 110, 130, 50, 20
STATICTEXT #1.static1, "Miles driven?", 10, 10, 120, 20
STATICTEXT #1.static2, "Fuel added?", 10, 35, 120, 20
STATICTEXT #1.static3, "", 10, 60, 130, 60
TEXTBOX #1.tbx1, 140, 0, 50, 25
TEXTBOX #1.tbx2, 140, 30, 50, 25
OPEN "Miles Per Gallon" FOR WINDOW AS #1
PRINT #1, "trapclose [quit]"
PRINT #1, "font verdana 10 bold"
[redo] 'Return here following an error
PRINT #1.tbx1, "!setfocus"
WAIT
[calc]
PRINT #1.tbx1, "!contents? miles"
PRINT #1.tbx2, "!contents? fuel"
IF miles=0 OR fuel=0 THEN
NOTICE "ERROR!"+CHR$(13)+"You left one or more fields blank!"+ _
CHR$(13)+"Please try again." : GOTO [redo]
END IF
PRINT #1.static3, "You averaged "; (miles/fuel); " miles per gallon."
WAIT
[quit]
CLOSE #1
END
I usually write DEMO programs without a GUI because an INPUT command does the same thing when programming in the MAINWIN, so the following routine shows use of the WAIT statement in two places. Note also that, like all Windows programs, you can TAB through the textboxes and buttons using the TAB key and do not need to use your mouse to position the cursor.
First, we must use WAIT before the [calc] branch routine. If we don't halt program execution and wait for user input, the program tries to execute the calculation with no input and a division by zero error would result, except that we checked for that with an IF/THEN comparison. If no value has been input, the program displays an error notice, then branches back to [redo]. Why didn't we branch back to [calc] following the error? Okay, you try it your way... I'll wait while you execute the "three fingered salute."
Now you know why we branched to [redo]. There is no substitute for learning by doing.
We use the WAIT statement upon completion of the calculation, and before the [quit] branch, or else the program would execute the code contained in [quit] and close abruptly. We don't want the program to close before we click the Exit button.
Another place where you must ALWAYS use the WAIT statement is in a TIMER routine.
TIMER 1000, [continue] 'Go here after 1000 milliseconds
WAIT 'Wait for the timer event.
[continue]
TIMER 0 'Turn the timer off or it will continue to fire each 1000 milliseconds.
GOTO [someWhereElse] 'Go here after the timer fires.
Now you've seen a few simple examples to create a GUI for your user, get input from a textbox, check for input errors, calculate and display results, exit your program, and turn off a TIMER if used. Some program flow may necessitate turning off the TIMER in the next branch routine. You will not encounter an error if you turn off a TIMER that has not been set.
The WAIT command is usually placed before each branch label. Code following a branch label will always be executed. The WAIT command means we cannot get to the branch routine without an outside event from the program or the user.
You will find additional tutorials for Getting Text Input from Your User; Branch Labels; The TIMER Statement; and TWEAK Your GOOEY on the JB Tutorials Board,