Hello, in this tutorial, am going to show you how to develop vb.net calculator in just few lines of code. Attachment is available for download.
Though this calculator can be developed using advance VB.Net coding, I have made it simple as I can for you and I to understand how the application will work without having to stress ourselves thinking of advance codes.
The program is been divided into two procedures which are and the . The procedure is used to key in value that will be display on the calculator's screen while the procedure otherwise is used to perform calculation process and algorithm.
Some variables like num1, num2, res and opt e.t.c where declared generally.
Just as I said, I have made this program as simple as possible.
Let's start right away
1. Loading the Visual Studio environment
First of all, you should launch Visual Studio by either clicking on Start menu button or clicking on its icon from the desktop. Whichever way you use, you should see a similar screenshot below depending on the version of Visual Studio you have mine is 2012.
2. Click on File menu, then click on New>Project and similar window as below will show
3. Enter the File Name in this case "Calculator" in the file name box
4. Specify folder to save to
5. Click on Ok
And a blank form as below should show
**Change the backcolor of the form to crimson and place every other controls inside group control in VB.net
Now add all controls on the table below and set properties as follows
Controls |
Properties |
Label |
Name: Label2
Text: Blank
Backcolor: crimson
ForeColor: White Font-Weight:bold |
TextBox |
ReadOnly:True
Text:0 |
Button1 |
Name: btn8
Text: 8
Backcolor:grey |
Button2 |
Name: btn9
Text: 9
Backcolor:grey |
Button1 |
Name: btn7
Text: 7
Backcolor:grey |
Continue adding the buttons until you have a design as shown below
Double click any of the control and add the codes below to its equivalent control.
Public Class Form1
Dim opt As String
Dim num1, num2, iVal As Integer
Dim res As Double
Dim data As String
Private Sub btn8_Click(sender As Object, e As EventArgs) Handles btn8.Click
AddDigits(8)
End Sub
Private Sub btn9_Click(sender As Object, e As EventArgs) Handles btn9.Click
AddDigits(9)
End Sub
Sub AddDigits(dVal As Integer)
If txtdisplay.Text = String.Empty Then
txtdisplay.Text = dVal
data = data & dVal
ElseIf Int(txtdisplay.Text) = "0" Then
txtdisplay.Text = dVal
data = data & dVal
Else
txtdisplay.Text = txtdisplay.Text & dVal
data = data & dVal
End If
Label2.Text = data
End Sub
Sub Process(opt1 As String)
If opt1 <> "=" Then
num1 = Convert.ToInt32(txtdisplay.Text)
txtdisplay.Text = ""
opt = opt1
data = data & txtdisplay.Text & opt
Label2.Text = data
Else
num2 = Convert.ToInt32(txtdisplay.Text)
If opt = "+" Then
res = num1 + num2
ElseIf opt = "/" Then
res = num1 / num2
ElseIf opt = "*" Then
res = num1 * num2
Else
res = num1 - num2
End If
data = data & "=" & res
Label2.Text = data
txtdisplay.Text = res
End If
End Sub
Private Sub btn7_Click(sender As Object, e As EventArgs) Handles btn7.Click
AddDigits(7)
End Sub
Private Sub btn4_Click(sender As Object, e As EventArgs) Handles btn4.Click
AddDigits(4)
End Sub
Private Sub btn5_Click(sender As Object, e As EventArgs) Handles btn5.Click
AddDigits(5)
End Sub
Private Sub btn6_Click(sender As Object, e As EventArgs) Handles btn6.Click
AddDigits(6)
End Sub
Private Sub btn1_Click(sender As Object, e As EventArgs) Handles btn1.Click
AddDigits(1)
End Sub
Private Sub btn2_Click(sender As Object, e As EventArgs) Handles btn2.Click
AddDigits(2)
End Sub
Private Sub btn3_Click(sender As Object, e As EventArgs) Handles btn3.Click
AddDigits(3)
End Sub
Private Sub btn0_Click(sender As Object, e As EventArgs) Handles btn0.Click
AddDigits(0)
End Sub
Private Sub btnplus_Click(sender As Object, e As EventArgs) Handles btnplus.Click
Process(btnplus.Text)
End Sub
Private Sub btndivide_Click(sender As Object, e As EventArgs) Handles btndivide.Click
Process(btndivide.Text)
End Sub
Private Sub btnmultiply_Click(sender As Object, e As EventArgs) Handles btnmultiply.Click
Process(btnmultiply.Text)
End Sub
Private Sub btnequalto_Click(sender As Object, e As EventArgs) Handles btnequalto.Click
Process(btnequalto.Text)
End Sub
Private Sub btnsubstract_Click(sender As Object, e As EventArgs) Handles btnsubstract.Click
Process(btnsubstract.Text)
End Sub
End Class
Here is a screenshot of the tested calculator program.
Please kindly scan any attachment downloaded here and do not open .exe or .ocx files. Only run source code. Emmason is not liable for any issues that may arise due to virus or whatever.