GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing wiki.appstudio.dev — Adding Two Numbers - NSB App Studio Adding Two Numbers
Jump to navigation
Jump to search
IntroductionThe purpose of this tutorial is to demonstrate simply adding two numbers together using App Studio. 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
The form and the code below Form1 won't be used in this example. Adding Two Numbers Using MsgBoxTo 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 MsgBox. Creating the CodeFirst we declare three variables to hold our values:
Next we set FirstNumber to equal 10 and SecondNumber to 7. We then add the two numbers and store the result in Total. Finally, we display the result in a MsgBox. Your code should look like the following: Sub Main
Dim FirstNumber
Dim SecondNumber
Dim Total
FirstNumber = 10
SecondNumber = 7
Total = FirstNumber + SecondNumber
MsgBox "And the total is: " & Total
End Sub
Saving the ProjectSave the project by selecting File from the top menu and select Save Project, or click on the disk symbol in the toolbar. Testing the ProgramSelect Start in Desktop Browser from the Run menu (or press F5, or click on the Start in Desktop Browser button in the toolbar). If you deploy this program to an iPhone, it will look like this:
Adding Two Numbers Using a FormDisplaying 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. Creating the FormStart App Studio from the Start menu. It will open the program you entered previously, Add2MsgBox.nsx. Select File and New Project, save it under the name Add2Form.nsx. Add a text box for entering the first number.
Click on the label object in the Toolbox ("Lb" icon). This is located under Common. A new label object will show in the top left corner of the form.
Drag a TextBox object from the ToolBox to the Design Screen.
Drag a Label object from the ToolBox to the Design Screen.
Drag a TextBox object from the ToolBox to the Design Screen.
Drag a Label object from the ToolBox to the Design Screen.
Add a button to add the two numbers:
Saving the ProjectSave the project by selecting File from the top main and select Save Project. Testing the FormAt this point we have a finished form but have not entered any code to actually add the numbers and display the results. You can run the program to see how the form will appear on a device or in the browser. You may find that some tweaks are needed in the size and position of some of the controls and labels.
Creating the CodeWe 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. App Studio will automatically open the Code Window and generate the Add button subroutine when you select the Events drop down box in the properties window of the button and choose 'onclick'. You can also open the Code Window by clicking on Code under Form1 in the Project Explorer. Enter the code so it looks like the following screen. Sub cmdAdd_onclick() Dim FirstNumber Dim SecondNumber Dim Total FirstNumber = Number(txtFirstNo.value) SecondNumber = Number(txtSecondNo.value) Total = FirstNumber + SecondNumber txtTotal.value = Total End Sub
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 and not storing anything. txtTotal.value = Number(txtFirstNo.value) + Number(txtSecondNo.value) Either way works but I feel the program is easier to understand when the variables are used. Testing the ProgramSelect Start in Desktop Browser from the Run menu (or press F5).
Error CheckingThe above program is very simple in terms of its 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 entered 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. Setting the textbox type to number switches the keyboard to numbers initially, but it is possible to switch it to letter input! Or the user might be using an external keyboard, which would also allow him to enter letters into the numeric fields. The final program with error checking: Sub cmdAdd_onClick()
Dim FirstNumber
Dim SecondNumber
Dim Total
If Not IsNumeric(txtFirstNo.value) or txtFirstNo.value="" Then
MsgBox "First Number must be a number!",0
Exit Sub
End If
If Not IsNumeric(txtSecondNo.value) or txtSecondNo.value="" Then
MsgBox "Second Number must be a number!",0
Exit Sub
End If
FirstNumber = Number(txtFirstNo.value)
SecondNumber = Number(txtSecondNo.value)
Total = FirstNumber + SecondNumber
txtTotal.value = Total
End Sub
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |