How to create a simple calculator in VB.Net
you can design the form very nice but it is the actual code that matters most not the interface
What you need to know is that the properties window is divided into parts. My concentration is on the design and click part
When you are done with the design.Double click on the button that has the text 1 and you will see something like this
You Need to declare two variables in the class so that they will be accessible in all the objects
Dim firstnumber As Integer or Decimal but don’t declare as a string.
You will be performing logical operations thus Addiction ,multiplication etc
Declare that too
Dim logic As integer >>>>>>>> Reff The image above
now you’ve declared everything in the class so in the sub-class of button1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Text = TextBox1.Text & 1 ****(it means write 1 but don’t delete whats in textbox1 )******
End Sub
Double click on 2 and change 1 to 2
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox1.Text = TextBox1.Text & 2****(it means write 2 but don’t delete whats in textbox1 )******
End Sub
Do same to 3,4,5,6,7,8,9,0>>>>>> but Do this for the Dot
Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
TextBox1.Text = TextBox1.Text & (“ . ”) ****(it means write . but don’t delete whats in textbox1 )******
End Sub
And for the Clare Button
Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
Application.Restart() ‘application.restart works well here but you can use this code too ========>>> firstnumber=vbNull’
End Sub
Now we are done with the numbers lets code for the operators thus (+,-,*,/)
double click on + AND write this code
It will look like this
Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
If logic = 1 Then
firstnumber = Val(firstnumber) + Val(TextBox1.Text)
TextBox1.Text = “”
logic = 1
ElseIf logic = 2 Then
firstnumber = Val(firstnumber) — Val(TextBox1.Text)
TextBox1.Text = “”
logic = 1
ElseIf logic = 3 Then
firstnumber = Val(firstnumber) * Val(TextBox1.Text)
TextBox1.Text = “”
logic = 1
ElseIf logic = 4 Then
firstnumber = Val(firstnumber) / Val(TextBox1.Text)
TextBox1.Text = “”
logic = 1
Else
If TextBox1.Text = “” Then
firstnumber = “1”
logic = 1
Else
firstnumber = TextBox1.Text
TextBox1.Text = “”
logic = 1
End If
End If
End Sub
Repeat for the — and make the logic 2
Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Click
If logic = 1 Then
firstnumber = Val(firstnumber) + Val(TextBox1.Text)
TextBox1.Text = “”
logic = 2
ElseIf logic = 2 Then
firstnumber = Val(firstnumber) — Val(TextBox1.Text)
TextBox1.Text = “”
logic = 2
ElseIf logic = 3 Then
firstnumber = Val(firstnumber) * Val(TextBox1.Text)
TextBox1.Text = “”
logic = 2
ElseIf logic = 4 Then
firstnumber = Val(firstnumber) / Val(TextBox1.Text)
TextBox1.Text = “”
logic = 2
Else
If TextBox1.Text = “” Then
firstnumber = “1”
logic = 2
Else
firstnumber = TextBox1.Text
TextBox1.Text = “”
logic = 2
End If
End If
End Sub
Do same for * and division but logic must be set 3 and 4 respectively
Now its time to program The (=) button
Double click on the = button and write this code
It will look like this
Private Sub Button17_Click_2(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button17.Click
If logic = 1 Then
If firstnumber = “1” Then
TextBox1.Text = 0 + Val(TextBox1.Text)
Else
TextBox1.Text = firstnumber + Val(TextBox1.Text)
End If
ElseIf logic = 2 Then
If firstnumber = “1” Then
TextBox1.Text = 0 — Val(TextBox1.Text)
Else
TextBox1.Text = firstnumber — Val(TextBox1.Text)
End If
ElseIf logic = 3 Then
If firstnumber = “1” Then
TextBox1.Text = 1 * Val(TextBox1.Text)
Else
TextBox1.Text = firstnumber * Val(TextBox1.Text)
End If
Else
If TextBox1.Text = 0 Then
TextBox1.Text = “Infenity”
ElseIf firstnumber = 0 Then
TextBox1.Text = “Syntax Error”
Else
If firstnumber = “1” Then
TextBox1.Text = 0 / Val(TextBox1.Text)
Else
TextBox1.Text = firstnumber / Val(TextBox1.Text)
End If
End If
End If
End Sub
How to program the off button
Double click on the off button and write this code
Me.Close()
This is how it will look like this
Private Sub Button18_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button18.Click
Me.Close()
End Sub
Thank You For Reading