|
Tutorial 02: Multiple Forms
May 01, 2008
© NSB Corporation. All rights reserved.
|
Purpose
The purpose of this tutorial is to help learn a few more
NS Basic/Symbian OS programming techniques. You should complete Tutorial #1 before
beginning this tutorial. In this tutorial, the techniques of using multiple
forms will be covered.
Description of the Program
The program to be developed has two forms. Form #1 allows
the user to enter a birthdate and name. Form #2 shows the values of the previously
entered birthdate and name plus it tells on what day of the week the birthdate
occurred.
Program Development
1. Startup
- Start NS
Basic/Symbian from the Start menu.
- At the initial
screen select New Project
2. Modify Project Properties
- Change the
(Name) to "MyProject".
3. Modify the First Form's Parameters
- You should
now see a blank form with the title "Form1"
- Select the
form in the Project Explorer by clicking on it.
- In the Properties
Window, change the Name to "My Form 1"
- Change the
Title to "Form to Enter Data" (This will print at the top of the
form)
- Set Default
Form to True
4. Build Form 1 Objects: Add a date selector for the birthdate
- Click on
the Selector object in the Toolbox . It's a small box with the letter trig
in it.
- Move the
cursor to the form where you want the object to appear and click the left
mouse button.
- Click on
the object to select it.
- In the Properties
Window, type "99/01/01" as the Label
- Set the Name
to "selDate"
Add a field to enter the user's name:
- Click on
the field object in the Toolbox (looks like "T")
- Click the
left mouse button to place the field object on the form
- Click on
the object to select it.
- In the Properties
Window, change the Name to "fldName"
Add a button to transfer to form #2
- Select the
button object ("OK") in the Toolbox.
- Place it
on the form and click on it to select it.
- In the Properties
Window. enter "Show the Day of the Week" as the Label.
5. Create the Second Form
- Select "Project"
from the top main menu
- Select "Add
Form" from the menu
- In the Properties
Window, set the Name to "My Form 2"
- Set
the Title to "Day of Week Display"
6. Create objects for Form 2
Add a text prompt before the birthday:
- Select the
Label ("T:") object from the Toolbox
- Place on
the form and click on it to select..
- In the Properties
Window, enter "Birthday=" as the label.
Add a field where the birthday will be shown:
- Select the
field object from the Toolbox
- Place on
the form and select it
- In the Properties
Window, change the Name to "fldDate2"
Add a text prompt before the name :
- Select the
label object from the Toolbox
- Place on
the form and select it
- In the Properties
Window, enter "Name=" as the label
Add a field where the name will be shown:
- Select the
field object from the Toolbox
- Place on
the form and select it
- In the Properties
Window, change the name to "fldName2"
Add a text prompt before the day-of-the-week:
- Select the
label object from the Toolbox
- Place on
the form and select it
- In the Properties
Windows, enter "Day-of-the-week=" as the Label
Add a field where the day-of-the-week will be shown:
- Select the
field object from the Toolbox
- Place on
the form and select it
- In the Properties
Window, change the name to "fldDayNumber"
Add a button to allow transfer back to form # 1:
- Select the
button object from the Toolbox
- Place it
on the form and select it
- In the Properties
Window, enter "Go Back" as the Label
7. Enter Code for Program Startup
- Under the
Project Menu, select " Startup Code"
- Enter these
lines of code after the Project_Startup() line
Global MyName as String
Global MyDate as date
MyDate=ToDate("1999/01/01")
The purpose of this code is to supply global variables for MyName and MyDate
so these values can be used throughout the program in multiple code sections
Give MyDate a valid date value because it is used in a date popup command
which requires that the date be valid. The user will actually select their own
birthday in the date popup routine and overwrite this date.
No further action is required. NS Basic/Symbian OS automatically displays the program's
first form at the start of program execution.
- Close the
code window and return to a display of the form.
The Project Explorer now shows "(Startup)" next to the project name
to indicate that there has been program code supplied there.
8. Provide Code for Form 1 Objects
Set Form 1 as the current form displayed by right clicking on it in the Project
Explorer and selecting "View Object"
Code to cause the popup date entry form to appear:
When the user taps the birthday selector on Form 1, we want
the standard date selector popup to appear. We must supply code
to the selector object on Form 1 to cause this to happen.
- Double click
on the birthday selector object to bring up the Code Window/
- Enter these
lines of code after the SUB line
Dim result as integer
result=PopupDate(MyDate,"What is your birthday?")
selDate.text=str(MyDate)
This code does the following:
- Defines a
"result" variable. The PopupDate function that is used in line 2
gives back a result to indicate if the user changed the date or not ( result=1
if yes or 0 if not). This variable has to be of type integer. This program
doesn't use the result for anything but the use of the PopupDate function
requires it.
- Calls the
PopupDate function to get the birthday entered by the user. This will revise
the date stored in MyDate.
- Puts the
value of the new date into the form object birthday selector field.If this
was not done, the old date value would remain visible. The str function converts
the internal date format of the birthday in MyDate to text string of the form
"yy/mm/dd"
9. Code to transfer to Form 2
When the user taps on the "Show the Day of the Week"
button, we want Form 2 to appear.
- Double click
on the button to bring up the Code Window
- Enter these
lines of code after the SUB line:
MyName= fldName.text
NextForm "My Form 2"
This code does the following:
- When Form
1 disappears, any info on it that the user entered will no longer be available
unless saved in some global variable. Since the value entered as the user's
name has not been previously saved, it is saved before exiting. The FldName.text
expression pulls the name value off of the form and puts it in the global
variable MyName so that it can be used in the code later in Form 2.
- The NextForm
command transfers control to Form 2
10. Provide Code for Form 2 Objects
- Right click
on Form 2 in the Project Explorer and select View Object.
Supply code to be executed after the form is drawn
- Right click
on Form 2 in the Project Explorer and select View After Code
- Enter these
lines of code after the SUB line
fldName2.text=MyName
fldDate2.text=dateMMDDYY(MyDate)
fldDayNumber.text=str(DayOfWeek(MyDate))
This code fills in form fields with values before the form displays.
- It sets the
field on the form that contains the user's name to the value from the global
variable MyName. This value is what the user entered on Form 1.
- It sets the
field on the form which contains the birthday to the value from the global
variable MyDate. This is the value entered on Form 1 by the user using the
date popup routine.
- It sets the
field on the form that contains the Day-of-the-week which is a number from
1 to 7. It does this by using the function DayOfWeek and supplying it as input
the birthday in MyDate. Since the function returns a number as a result, this
must be translated to a text string using the str function before it can be
assigned to the text field, which must be a string.
11. Supply code for the Go Back button to go back to Form 1
- Double click
on the button to bring up the Code Window
- Enter this
code after the SUB line
NextForm "My Form 1"
When this button is selected, the program will just cause
Form 1 to re-appear and the user can try another date.]
This will save your project as c:\NSBasic\MyProject.prj.
All form definitions, form objects, all code, and other project info are saved
into the one project file.
12. Generate the Installer
- Select Compile from the Run menu.
If you have any errors, an error message will appear and
the code window will appear with the section of code where the error occurred
highlighted. Errors must be corrected and Compile repeated until there are no
more errors. If you do not have errors, a message box will appear showing the
size of the compiled application.
If you corrected any errors, you should re-save the project. This action
creates a downloadable file in the c:\NSBasic\Download directory.
c:\NSBasic\Download\myProject.sisx
The file must be downloaded to the device to execute your project.
Congratulations !! You have now written your second NS Basic/Symbian OS
program.
Testing The Project
First, make sure your device is connected to your computer using the Nokia PC Suite or equivilent.
- Go into c:\nsbasic\downloads
- Double click on myProject.sisx.
- Install as you would any other application.
- If you get "Certificate Error. Contact the Application Supplier", do the following and then install again.
- On the device, go to Tools
- App. Manager
- Highlight App. downllds.
- Click "Options"
- Select "Settings"
- Software installation: change from "Signed only" to "All"
- Run the app on the device