Adding BASIC Code
There's more to a program than just components. Programs don't just consist of buttons on a form. What makes a program useful is how it reacts when the user presses a button on a form. You will specify how your program responds to user and system input by writing routines in a BASIC-like programming language.
Your program detects user and system input through its components: buttons can tell when the user clicks them, text entries can tell when the user enters text in them, timers detect ticks of the system clock. Components generate events, and you can write BASIC routines that will execute when an event is generated. These routines are known as
event handlers
.
How to edit an event handler
To edit an event handler, follow these steps:
-
Open the Editor window. If it's not visible, then choose Editor from the Window menu. The Editor window will look something like the window pictured to the right.
-
In the Editor window, there's a list called Components. Select the name of the component whose event you want to handle.
-
There's another list in the Editor window, a list called Events. This contains the names of the events that the selected component can generate. Depending on a component's type, it might generate just one kind of event, several kinds of events, or no events. Choose the name of the event you want to handle.
-
In a few seconds, the text entry area that takes up most of the space of the Editor window will show you the BASIC code for that event handler. By typing BASIC code in the routine, you edit the event handler.
-
If you do something else in the Builder--select another handler to edit, run your program, save your program--the Builder will parse the routine you've been editing to check its BASIC syntax and look out for other errors. If it detects errors, it will display an error dialog box and highlight the line of BASIC code that seems wrong.
For example, let's make an event handler for the Make Funky button we've been working on:
-
Choose Editor from the Window menu. After a few seconds, the Editor window should appear.
-
In the Components list in the Editor window, select the button1 item.
-
After a couple of seconds, the Events list should contain one item, called pressed. Select it.
-
After a couple of seconds, the text area of the Editor window should contain the text
SUB button1_pressed( self AS button )
END SUB
-
Click on the blank text line between the "SUB..." and "END SUB" lines to position the text cursor. Enter text so that the event handler reads
SUB button1_pressed( self AS button )
form1.caption = "Funky"
END SUB
-
We want the Editor to check your routine to make sure you haven't made any syntax errors. Choose module_init() from the Routines list in the Editor window. At this point, two things can happen: either the module_init() subroutine will appear in the Editor's text window, or else an error dialog box will appear telling you that there was a syntax error in the button1_pressed() routine. If there's no error, then you're done. But...
-
If there was an error
-
Note the error message in the error dialog box. It might give you a hint about what went wrong.
-
Press OK in the error dialog box.
-
Look at the Editor text area. There should be a line of BASIC code highlighted. There is a good chance that the syntax error is on this line.
-
Check over the BASIC code you wrote. Does it appear as shown in step 5, above? It should read form1<period>caption<space><equals-sign><space><double-quote>Funky<double-quote>. It should all be on the same line of text.
|
About the edit window
You may also create and edit routines that are not event handlers--as you will see, routines can call other routines. To edit an existing routine, you may choose its name from the Routines list in the Editor window. To create a new routine, press the New Sub or New Function button.
The chapter of this manual which describes the BASIC programming language, starts on
See The NewBASIC Language
.