You are on page 1of 14

Basic Calculator Tutorial in Visual

Basic .NET - Page 3


Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013

Page3
Go to the code page and add the following declarations:

?
1 Dim value1 As String = ""
2 Dim value2 As String = ""
3 Dim value3 As Double = 0.0

Clear Button Code


?
1 Private Sub Button18_Click(sender As System.Object, e As System.EventArgs) Handles
2 Button18.Click
value1 = ""
3 value2 = ""
4 value3 = 0.0
5 Button1.Text = 0.0
6 End Sub

Exit Button Code


?
1 Private Sub Button19_Click(sender As System.Object, e As System.EventArgs) Handles
Button19.Click
2 End
3 End Sub

Equal Button Code


?
1 Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Han
2
3 Button16.Click
If value1 > "" And value2 = "+" Then
4 Button1.Text = Val(value1) + Val(Button1.Text)
5 value3 = Button1.Text
6 ElseIf value2 > "" And value2 = "-" Then
7 Button1.Text = Val(value1) - Val(Button1.Text)
8 value3 = Button1.Text
ElseIf value2 > "" And value2 = "*" Then
9 Button1.Text = Val(value1) * Val(Button1.Text)
10 value3 = Button1.Text
11 ElseIf value2 > "" And value2 = "/" Then
12 Button1.Text = Val(value1) / Val(Button1.Text)
value3 = Button1.Text
13 Else
14 End If
15 End Sub
16

Addition, Subtraction, Multiplication and Divition


Buttons Code will be handled under one sub
?
1 Private Sub Button17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Han
Button17.Click, Button13.Click, Button9.Click, Button5.Click
2 value2 = sender.text
3 value1 = Button1.Text
4 End Sub

Zero Button Code


?
1
Private Sub Button14_Click(sender As System.Object, e As System.EventArgs) Handles
2 Button14.Click
3 If Button1.Text = value1 Then
4 Button1.Text = "0."
5 ElseIf Button1.Text = "0." Then
Button1.Text = "0."
6 ElseIf Button1.Text = "0" Then
7 Button1.Text = "0."
8 ElseIf Button1.Text = value1 Then
9 Button1.Text = "0."
10 Else : Button1.Text = Button1.Text & "0"
End If
11 End Sub
12

Decimal Button Code


?
1
2 Private Sub Button15_Click(sender As System.Object, e As System.EventArgs) Handles
Button15.Click
3 If Button1.Text = "0." Then
4 Button1.Text = "."
5 ElseIf Button1.Text = value3 Then
6 Button1.Text = "."
ElseIf Button1.Text = value1 Then
7 Button1.Text = "."
8 Else
9 If Button1.Text.Contains(".") Then
10 Else
11 Button1.Text = Button1.Text & "."
End If
12 End If
13 End Sub
14

Numbers 1 - 9 Buttons Code under one sub:


?
1 Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles
2 Button8.Click, Button7.Click, Button6.Click, Button4.Click, Button3.Click, Button2.Clic
3 Button12.Click, Button11.Click, Button10.Click
If Button1.Text = value1 Then
4 Button1.Text = sender.text
5 ElseIf Button1.Text = "0." Then
6 Button1.Text = sender.text
7 ElseIf Button1.Text = value3 Then
Button1.Text = sender.text
8 Else
9 Button1.Text = Button1.Text & sender.text
10 End If
11 End Sub

- See more at: http://www.visual-basic-


tutorials.com/form/CalculatorT3.htm#sthash.0wUO4xzd.dpuf

Basic Calculator Tutorial in Visual


Basic .NET
Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013
This tutorial will show you how to make a basic calculator in visual basic .net.
Source Code for the calculator project is available at the end of page 3 of this
tutorial.

Create a new project.


You can change the form text to "your name's calculator". Click on
the form and then go to propertieswindow, then change the text property.
You also need to disable the maximize control of the form. That's because
you don't want the user to maximize the calculator. Go to
the properties window, and change the maximize box property to False:

You also need to change the FormBorderStyle property to Fixed3D

Add a button to the form and place on top of the form:


You might be surprised of this button because we should have placed a
textbox instead. Well, this is the cool thing about Visual Basic, you can use a
button to display text and results.
Now we have to change the button properties to look like a text box.
First we change it's text to "0.".

We align the text to be on the right. Right click on the button, click on
properties. In the properties window change TextAlign to MiddleRight:
Choose the box in the middle right (the blue one in the image above)

Change the FlatStyle of the button. In the properties window


change FlatStyle to Flat:

You also need to change the color of the button. Go to the properties window
and change the backcolor property to white:
There are two more properties you need to
change: TabStop to False and Enabled to False.

Your button should look like this now:


- See more at: http://www.visual-basic-
tutorials.com/form/CalculatorT.htm#sthash.A7Desnyh.dpuf

Basic Calculator Tutorial in Visual


Basic .NET - Page 2
Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013

Page 2
Page: 1 2 3

Let's add the rest of the buttons to the form.


Important: please add the button in the same order exactly as shown below.
Add four buttons. Start by adding the button on the left (1) then (2) then (3)
then (4)

Change the text of the four buttons to 7 , 8, 9, / as shown above.


Add four more buttons and change their text to 4, 5, 6, *
Remember to start adding the buttons in order as the following image:

Add four more buttons in the same order as shown below and change their
text to 1, 2, 3, -
Add four more buttons in the same order as shown below and change their
text to 0, . , =, +

Add the last two buttons to the calculator in order as shown below and change
their text to Clear, Exit:
Basic Calculator Tutorial in Visual
Basic .NET - Page 3
Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013

Page3
Go to the code page and add the following declarations:

?
1Dim value1 As String = ""
2Dim value2 As String = ""
3Dim value3 As Double = 0.0

Clear Button Code


?
1Private Sub Button18_Click(sender As System.Object, e As System.EventArgs) Handles Button18.
2 value1 = ""
3 value2 = ""
4 value3 = 0.0
Button1.Text = 0.0
5
End Sub
6

Exit Button Code


?
1Private Sub Button19_Click(sender As System.Object, e As System.EventArgs) Handles Button19.
2 End
3End Sub

Equal Button Code


?
1
2
3
4
5 Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Hand
6 If value1 > "" And value2 = "+" Then
7 Button1.Text = Val(value1) + Val(Button1.Text)
8 value3 = Button1.Text
ElseIf value2 > "" And value2 = "-" Then
9 Button1.Text = Val(value1) - Val(Button1.Text)
1 value3 = Button1.Text
0 ElseIf value2 > "" And value2 = "*" Then
11 Button1.Text = Val(value1) * Val(Button1.Text)
value3 = Button1.Text
1
ElseIf value2 > "" And value2 = "/" Then
2 Button1.Text = Val(value1) / Val(Button1.Text)
1 value3 = Button1.Text
3 Else
1 End If
End Sub
4
1
5
1
6

Addition, Subtraction, Multiplication and Divition


Buttons Code will be handled under one sub
?
1Private Sub Button17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handle
2 value2 = sender.text
3 value1 = Button1.Text
End Sub
4

Zero Button Code


?
1
2
3 Private Sub Button14_Click(sender As System.Object, e As System.EventArgs) Handles Button14
4 If Button1.Text = value1 Then
Button1.Text = "0."
5 ElseIf Button1.Text = "0." Then
6 Button1.Text = "0."
7 ElseIf Button1.Text = "0" Then
8 Button1.Text = "0."
ElseIf Button1.Text = value1 Then
9
Button1.Text = "0."
1 Else : Button1.Text = Button1.Text & "0"
0 End If
11 End Sub
1
2

Decimal Button Code


?
1 Private Sub Button15_Click(sender As System.Object, e As System.EventArgs) Handles Button15
If Button1.Text = "0." Then
2 Button1.Text = "."
3 ElseIf Button1.Text = value3 Then
4 Button1.Text = "."
5 ElseIf Button1.Text = value1 Then
6 Button1.Text = "."
Else
7 If Button1.Text.Contains(".") Then
8 Else
9 Button1.Text = Button1.Text & "."
1 End If
0 End If
End Sub
11
1
2
1
3
1
4

Numbers 1 - 9 Buttons Code under one sub:


?
1
2 Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button8.C
3 Button12.Click, Button11.Click, Button10.Click
If Button1.Text = value1 Then
4 Button1.Text = sender.text
5 ElseIf Button1.Text = "0." Then
6 Button1.Text = sender.text
7 ElseIf Button1.Text = value3 Then
Button1.Text = sender.text
8 Else
9 Button1.Text = Button1.Text & sender.text
1 End If
0 End Sub
11
- See more at: http://www.visual-basic-
tutorials.com/form/CalculatorT3.htm#sthash.wJWfuTHw.dpuf

You might also like