Welcome to Basic4ppc community!
In this tutorial we will take you step by step as you build your first (simple) application.
The screen shots are taken from the desktop IDE but it is basically the same as building it on the device IDE.
The code is attached to this post.
Lets start:
Open Basic4ppc.
Start with building the GUI.
Choose Designer - Create New Form
Using the Controls menu, add one Button, one TextBox and one Label.
Move and resize the controls by dragging them with the mouse.
Important controls should have a meaningful name, so change Button1 name to btnGuess and TextBox1 name to txtNumber.
Change the controls' text to match the above screen shot.
Basic4ppc is an event driven language, which means that the application waits for the user to do something and then executes the relevant code.
Add a button click event to btnGuess by choosing the button and selecting Events - Click.
This will create a sub named btnGuess_Click in your code.
Note that you could also manually create a sub with the same signature and it will handle the event.
Now it's time to write the code.
Variables could be either global or local.
Local variables could only be used inside their sub and they are recreated in each call.
Global variables could be used anywhere. Global variables should be declared inside Sub Globals.
It is not required to use the Dim keyword with regular variables (not arrays / structures). You could just write number = 0 or number = 23 instead of Dim number.
Note that regular variables are variant variables, they can hold both numbers and strings.
Back to our code. We declare a global variable named number.
The first sub that always run is Sub App_Start.
We do two things in this sub, we show Form1 and we set number value to be a random number between 1 to 100 (actually 99).
Now we handle the btnGuess click event.
First we check that the user text is a number using the IsNumber method.
If it is not we show a msgbox and return.
If it is a number it is compared to our number.
You could now run your code by pressing F5 or choosing Debug - Run.
We now improve our game by allowing the user to play another game and by returning the focus to txtNumber after each guess.
Using the fact that Msgbox returns a value based on what the user chose, we check if the user pressed on the Yes button (cYes is a constant).
Another important constant that is used here is crlf which adds the newline characters to the string.
If the user pressed on the No button we close the application using AppClose.
AppClose closes the main form (exactly the same as Form1.Close in our case).
A little bit about debugging (only on the desktop):
You could add breakpoints to your code, breakpoints are lines that will stop the execution and will allow you to check any important values.
Adding a breakpoint is done by pressing on the left bar.
Now when we press on btnGuess the execution will stop.
It is not clear in the screen shot, but the mouse hovers over the number variable and it shows 42.
By hovering over any variable you could see its value.
More complicated values could be seen using the Watches at the bottom of the screen.
To continue running your code you can press F5, if you want to continue step by step press F8 (F9 also continues step by step without getting into deeper subs, F10 continues step by step but only after getting out of the current sub).
In this tutorial we will take you step by step as you build your first (simple) application.
The screen shots are taken from the desktop IDE but it is basically the same as building it on the device IDE.
The code is attached to this post.
Lets start:
Open Basic4ppc.
Start with building the GUI.
Choose Designer - Create New Form
Using the Controls menu, add one Button, one TextBox and one Label.
Move and resize the controls by dragging them with the mouse.
Important controls should have a meaningful name, so change Button1 name to btnGuess and TextBox1 name to txtNumber.
Change the controls' text to match the above screen shot.
Basic4ppc is an event driven language, which means that the application waits for the user to do something and then executes the relevant code.
Add a button click event to btnGuess by choosing the button and selecting Events - Click.
This will create a sub named btnGuess_Click in your code.
Note that you could also manually create a sub with the same signature and it will handle the event.
Now it's time to write the code.
Variables could be either global or local.
Local variables could only be used inside their sub and they are recreated in each call.
Global variables could be used anywhere. Global variables should be declared inside Sub Globals.
It is not required to use the Dim keyword with regular variables (not arrays / structures). You could just write number = 0 or number = 23 instead of Dim number.
Note that regular variables are variant variables, they can hold both numbers and strings.
Back to our code. We declare a global variable named number.
The first sub that always run is Sub App_Start.
We do two things in this sub, we show Form1 and we set number value to be a random number between 1 to 100 (actually 99).
Now we handle the btnGuess click event.
First we check that the user text is a number using the IsNumber method.
If it is not we show a msgbox and return.
If it is a number it is compared to our number.
You could now run your code by pressing F5 or choosing Debug - Run.
We now improve our game by allowing the user to play another game and by returning the focus to txtNumber after each guess.
Using the fact that Msgbox returns a value based on what the user chose, we check if the user pressed on the Yes button (cYes is a constant).
Another important constant that is used here is crlf which adds the newline characters to the string.
If the user pressed on the No button we close the application using AppClose.
AppClose closes the main form (exactly the same as Form1.Close in our case).
A little bit about debugging (only on the desktop):
You could add breakpoints to your code, breakpoints are lines that will stop the execution and will allow you to check any important values.
Adding a breakpoint is done by pressing on the left bar.
Now when we press on btnGuess the execution will stop.
It is not clear in the screen shot, but the mouse hovers over the number variable and it shows 42.
By hovering over any variable you could see its value.
More complicated values could be seen using the Watches at the bottom of the screen.
To continue running your code you can press F5, if you want to continue step by step press F8 (F9 also continues step by step without getting into deeper subs, F10 continues step by step but only after getting out of the current sub).