Pages

Friday 29 March 2013

Adding Two Numbers Using Windows Form Application in Visual Basic.Net


Adding Two Numbers must  need the following objects:
  • 2 Textbox - to input the two numbers to add.
  • 4 Label - 1 label object is to returns the total of the two numbers added and the 3 Labels are use as the caption of the Textbox.
  • 1 Button - to compute the two numbers that is going to add.
Step 1
    Drag all the objects listed above into the Form.

                        The image below is the output of the created object.


Step 2
      Change the Text or caption and name of your objects so that you must easily to identify each objects. Always remember that the name of your objects provided is the name of the object when you used that object in your source code.

How to change the text or Caption and the name of your objects?

       1.  Right click the object and choose the Properties.
On the right side you have see the properties Tab of the object. If you want to change it then change on what name of object you want. To change the text or the Caption of your objects, just scroll down on the properties tab and find the text and change it want you want.

Step 3
        After change the Text and the Name of your textbox, Arrange it whatever you want. The image below shows the output of the program.


Note: The First Number, Second Number and Total has no function on the program but it just created to identify if what you need to input on the textbox.

Step 4
         The Program is not perform according to what you want to perform of your program without source code. Let's create a code to perform by our program.

Double Click the button named Compute. After double click you see the source code Arguments of the button objects.

Public Class Form1
    Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute.Click
       txttotal.Text = txtNum1.Text + txtnum2.Text

    End Sub
End Class

      
Now let's try to compute.
               First Number = 2
               Second Number = 2

Here is the output:

The Total is 22 which is wrong.

The Two numbers are concatenate because the default datatype of textbox is String. Read more About Concatenation. This error is called Logical error  because the program is still running but the output is wrong.

To solve this error is to put "Val" of it's Textbox named.
                        txttotal.Text = Val(txtNum1.Text) + Val(txtnum2.Text)
The Purpose of "Val" is to Return a number contained in a String as a numeric value of appropriate type.
 The total is 4 which means the code is correct.

Using other operator just like the Subtraction (-), Multiplication (*), Division is the same on how the code of Addition is written. Just change the Operator.

Code for Subtraction of two numbers in Windows Form Application using VB.net
Public Class Form1
    Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute.Click
       txttotal.Text = Val(txtNum1.Text) - Val(txtnum2.Text)

    End Sub
End Class



Code for Multiplication of two numbers in Windows Form Application using VB.net
Public Class Form1
    Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute.Click
       txttotal.Text = Val(txtNum1.Text) * Val(txtnum2.Text)

    End Sub
End Class


Code for Division of two numbers in Windows Form Application using VB.net
Public Class Form1
    Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute.Click
       txttotal.Text = Val(txtNum1.Text) / Val(txtnum2.Text)

    End Sub
End Class

2 comments: