Tutorial 03: Adding Two Numbers

April 18, 2008

© NSB Corporation. All rights reserved.

Contributed by Tom Newman, Mission Peak Designs


Contents:

  1. Introduction
  2. Getting Started
  3. Adding Two Numbers Using MessageBox
  4. Adding Two Numbers Using Form

Introduction

The purpose of this tutorial is to demonstrate simply adding two numbers together using NS Basic/CE. You should have completed Tutorial #1 before beginning this tutorial.

The program to be developed will start out adding two numbers and displaying the answer in a message box. After that we will create a program that allows the user to enter the two numbers on a form and display the answer when the Add button is clicked.

Getting Started

Start NS Basic/CE from the Start menu and select Formless from the project dialog box. This will open a code window.

Adding Two Numbers Using MessageBox

To demonstrate how simple it is to create a program to add two numbers together, we will enter code to add two numbers and display the answer in a MessageBox.

1. Creating the Code

First we declare three variables to hold our values:

Next we set FirstNumber to equal 10 and SecondNumber to 7.

We then add the two number and store the result in Total.

Finally, we display the result in a MessageBox using the CStr function to convert the number into a string we can display.

Your code should look like the following:

Option Explicit
ShowOKButton True

Sub main
   Dim FirstNumber
   Dim SecondNumber
   Dim Total
   
   FirstNumber = 10
   SecondNumber = 7
   
   Total = FirstNumber + SecondNumber
      
   MsgBox "And the total is: " + CStr(Total),0,"NS Basic"
   
End Sub

2. Saving the Project

Save the project by selecting File from the top main and select Save Project. Name the project Add2MsgBox.nsb and click Save

3. Testing the Program

Adding Two Numbers Using Form

Displaying the total of two fixed numbers in a MessageBox is not very practical since you need to modify the program every time you want to change the numbers. We will create a new version of the program that will allow the user to enter the numbers on a form and display the total when the user clicks a button.

1. Creating the Form

Start NS Basic/CE from the Start menu and select Standard from the project dialog box. (If NS Basic is already running, select File and New Project instead.) This will open a form window.

Add a text box for entering the first number: Add a text label before the first number: Add a text box for entering the second number: Add a text label before the second number: Add a text box for displaying the Total: Add a label before the Total field: Add a button to add the two numbers:

2. Saving the Project

Save the project by selecting File from the top main and select Save Project. Name the project Add2Form.nsb and click Save

3. Testing the Form

At this point we have a finished form but have not entered any code to actually add the numbers and display the results. You can still run the program to see how the form will appear on a CE device or the CE Emulator. You may find that some tweaks are needed in the size and position of some of the controls and labels.

Note: As a shortcut, you can use F5 to both Save and Start the program. If you are working with a new project, you will be prompted for a project name before the program is started.

Tip: If you're testing the form and haven't yet entered any code you will find the program doesn't really go away when you click the X box on the CE device. In order to stop a program, do the following: Start->Settings->System->Memory->Running Programs and selecting the program and Stop or Stop All. Adding "ShowOkButton True" in the Code Window will put an OK button in place of the X button and close the program.

If your program aborts with an error you may still need to stop the program using CE's Memory utility. If the program aborts before the ShowOKButton statement executes only the X button will appear on the CE screen.

4. Creating the Code

We are now ready to add code to our form to make it actually do some work. We will use the same variables we declared in our first example but read their values from the fields entered by the user. Reading the user's input and adding the numbers will be performed when the user clicks on the Add button.

NS Basic will automatically open the Code Window and generate the Add button subroutine when you double click the button on the form. You can also open the Code Window by right clicking on the form and selecting View Code.

Enter the code so it looks like the following screen.

Option Explicit
ShowOKButton True

Dim FirstNumber
Dim SecondNumber
Dim Total
   
Sub cmdAdd_Click   
   FirstNumber  = CSng(txtFirstNo.Text)
   SecondNumber = CSng(txtSecondNo.Text)
   
   Total = FirstNumber + SecondNumber
   
   txtTotal.Text = Total
End Sub

The CSng function is used to convert the number string the user enter into a single precision number.

Note: The code uses variables to hold the user's numbers and the total. You could also eliminate all the variables by directly accessing the fields directly and not storing anything.

	txtTotal.Text = CSng(txtFirstNo.Text) + CSng(txtSecondNo.Text)

Either way works but I feel the program is easier to understand when the variables are used.

5. Testing the Program

There are three TextBox fields on the form. The first two accept user input and the third (Total) has it's Locked property set to True, making it a read-only field. This property can be set in the Property window or as a line of code.

6. Error Checking

The above program is very simple in terms of it's function and lack of any type of error checking. As an additional exercise, code was added to verify that the user has entered the proper values when the Add button is clicked. The IsNumeric function is used to verify that the user has enter a proper number into both text box fields. An error message is displayed if the field is left blank or does not contain a valid number.

The final program with error checking:

Option Explicit
ShowOKButton True

Dim FirstNumber
Dim SecondNumber
Dim Total
   
Sub cmdAdd_Click
   If Not IsNumeric(txtFirstNo.Text) Then
      MsgBox "First Number must be a number!",0,"NS Basic"
      Exit Sub
   End If

   If Not IsNumeric(txtSecondNo.Text) Then
      MsgBox "Second Number must be a number!",0,"NS Basic"
      Exit Sub
   End If
         
   FirstNumber  = CSng(txtFirstNo.Text)
   SecondNumber = CSng(txtSecondNo.Text)
   
   Total = FirstNumber + SecondNumber
   
   txtTotal.Text = Total
End Sub