Tutorial 02: Multiple Forms

February 01, 2010

© NSB Corporation. All rights reserved.

Contributed by Tom Newman, Mission Peak Designs


Purpose

The purpose of this tutorial is to help learn a few more NS Basic/CE 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 birth date and name. Form #2 shows the values of the previously entered birth date and name plus it tells on what day of the week the birth date occurred. There is a button on each form that will switch between forms.

Program Development

1. Startup

2. Modify the First Form's Parameters

3. Create Objects for Form 1

Add a date selector for the birth date: Add a label for entering the birth date: Add a text box to enter the user's name: Add a button to transfer to Form 2: Name and save the project:

5. Create the Second Form

6. Create Objects for Form 2

Add a text label before the birthday: Add a field where the birthday will be shown: Add a text label before the name: Add a field where the name will be shown: Add a text label before the day-of-the-week: Add a field where the day-of-the-week will be shown: Add a button to transfer back to Form  1:

We have completed the visual design of our program so now it's time to add code to make it do something.

7. Enter Code on Form 1 to Transfer to Form 2

When the user taps on the "Show the Day of the Week" button, we want Form 2 to appear. The two lines are used to hide form 1 (unloads the form) and show form 2 (loads the form). No further action is required. NS Basic/CE automatically displays the program's first form (My_Form_1) at the start of program execution.

8. Enter Code for Form 2 Objects

We need to add code that will execute when Form 2 loads.
  lblDateField.Text = FormatDateTime(selDate.Date,2)
  lblNameField.Text = txtName.Text
  lblDayField.Text = WeekDayName( DatePart("w",selDate.Date)) 
This routine is called when the user clicks the Form 1 button which cause Form 2 to load. This code transfers the date and name from the first form to the second form. It also computes the day of the week from the date selected by the user. The FormatDateTime statement converts the Date and Time value to a short date string for displaying on the form.

9. Enter Code on Form 2 to Transfer to Form 1

When the user taps on the "Go Back" button, we want Form 1 to reappear.
  My_Form_2_Hide
  My_Form_1_Show 
This code hides Form 2 and shows (loads) Form 1.

10. Finishing the Code

Finish up the code for this project by entering the following lines of code at the top of the code page (before the first SUB line):
  Option Explicit
  ShowOKButton TRUE 
The two lines are located outside the subroutine and executes when the program first loads. The Option Explicit is used to force explicit declarations of all variables. This means all variables used in the program must be defined with a DIM statement before they are used. It helps to catch mistyped variable names and should be included in every program you write.

The ShowOKButton TRUE only applies to NS Basic/CE programs and places a OK button in place of the X button in the menubar of the output window. Tapping the X button minimizes the output window but doesn't stop the program. Tapping the OK button closes the output window and the program exits. The OK button is useful especially during program debugging because it allows the program to be closed so an updated version can be tested. Without the OK button the program will need to be shut down manually before an updated version can be tested. (You shut down running programs on PPC devices by Start->Settings->System->Memory->Running Programs and selecting the program and Stop or Stop All.) This statement can also be used on NS Basic/Desktop programs if the code is shared with /CE (the statement is a no-op on the Desktop).

Your code should look like the following:

Option Explicit
ShowOKButton True    ' Used for /CE, no-op for /Desktop

Sub cmdShow_Click
   My_Form_1_Hide
   My_Form_2_Show
End Sub

Sub My_Form_2_Load
   lblDateField.Text = FormatDateTime(selDate.Date,2)
   lblNameField.Text = txtName.Text
   lblDayField.Text = WeekDayName( DatePart("W",selDate.Date) )
End Sub

Sub cmdGoBack_Click
   My_Form_2_Hide
   My_Form_1_Show
End Sub

Testing the Project

Now we are ready to test the project. You can either download and run the program on a CE device or on the Microsoft Device Emulator. Any errors will appear in a message box on the CE device/emulator. The message box displays the error and line number where the error occurred. Errors must be corrected and Start repeated until there are no more errors.

Congratulations !! You have now written your second NS Basic/CE program.