VISUAL BASIC – CODING ADDITION AND EQUAL BUTTONS

CODING ADDITION BUTTON

The addition button will perform the following functions

  • Assign the value in the text box to a temporary storage to be used in the addition
  • Clear the text box so that we can enter the second number

Declare a global variable at the top immediately after calculator class as in diagram below.

This kind of of variable is accessible by all the codes in other button.

That means that these variables can be accessed not only by the addition and answer buttons but also the other buttons.

 

Dim temp1 as double

This is the variable that will temporary hold the value on the text box so that we can clear the text box to enter enter the second number.

Also declare answer as a global variable in the same manner as shown in the figure below

addition 1
addition 1

Now double click the addition button to bring the coding window for the button and enter the code below

temp1 = Val(TextBox1.Text)
TextBox1.Clear()

First we are assigning the value in textbox1 to temp1 and then clearing the text box so as to enter a second number.

Remember to test your program at every stage, it is more interesting to do so.

 



Coding for the answer button.

when the button is clicked the value in the textbox should be added to temp1 and the answer displayed in the textbox. Quite tricky though but simple enough.

Double click the answer button and enter the code below.

answer = temp1 + Val(TextBox1.Text)
TextBox1.Text = answer
temp1 = 0

note that after displaying the answer in the textbox we are resetting the temp1 to 0.

addition plus button
addition plus button

Now test your program and see that it is working properly.

Next we look at multiplication button.

You can review the previous lesson here SIMPLE CALCULATOR PROGRAM

You can also follow our tutorials on visual basic 2010

Feel free to leave your comments

Leave a Reply

Your email address will not be published. Required fields are marked *