VB NET Calculator
 

Designing the Form
What does a calculator need? numbers, display area for the result, A plus sign button, an equals sign button, and a clear the display button.

We'll have 10 button for the numbers 0 to 9. When a button is clicked its value will be transferred to a display area, which will be a Textbox.

Once a number is transferred to the Textbox we can click on the Plus button. Then we need to click back on another number.

To get the answer, we'll click on the equals sign.

To clear the display, we'll have a Clear button.

create a new project. Save it as Calculator.

To your new form, first add ten Buttons (You can add one, then copy and paste the rest)

The Buttons should have the following Properties:

Name: btn Plus a Number (btn1, btn2, btn3, etc)

Text: A number from 0 to 9 for each button

Font: MS Sans Serif, Bold, 14

Next, add a Textbox. Set the following properties for the Textbox:

Textbox
Name: txtDisplay
Font: MS Sans Serif, Bold, 14
Text: Erase the default, Textbox1, and leave it blank

Three more Command buttons need to be added

Plus Button
Name cmdPlus
Font MS Sans Serif, Bold, 14
Text +

Equals Button
Name cmdEquals
Font MS Sans Serif, Bold, 14
Text =

Clear Button
Name cmdClear
Font MS Sans Serif, Bold, 14
Text Clear




So far, when you've set up a variable, you've set them up behind a Private Subroutine. Like this:


Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnZero.Click


Dim MyVariable As String


End Sub


Suppose you had another button on the form, Button2, and the code was this


Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnZero.Click


Dim MyOtherVariable As String


End Sub


You cant access MyVariable from Button2?
Its a local variable


We need to put it outside of the code for a Button.
Then more than one Button can see the code.


place your variable declarations right at the top of the code window,
beneath the line  "Public Class Form1".


Declare two Public Integer variables total1 and total2:


The 0 to 9 Buttons

So double click the 0 to 9 Buttons and enter the following code:


Private Sub btn1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnZero.Click


txtDisplay.Text = btn1.Text


End Sub


This code will transfer the Text Property of a Button called btn1
to the Text Property of a Textbox called txtDisplay.


Run your programme and try it out. When the programme is running, click the 0 button to see that it does indeed transfer the Text on the Button to the textbox


add that code to all of your ten number Buttons with slight changes



Adding up
Click first on the 3
A 3 appear in the textbox
Click the + symbol
The 3 disappears from the textbox
Click on the number 7
A 7 appears in the textbox
Click on the = symbol
The 7 disappears from the textbox
The answer to 3 + 10 appears in the textbox
Click the "Clear" button to clear the textbox



So the whole code for our Button called btnPlus is this:


Private Sub btnPlus_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnPlus.Click


total1 = total1 + Val(txtDisplay.Text)


txtDisplay.Clear()


End Sub


 


 


 


 

(c) Shilpa Sayura Foundation 2006-2017