How to Make a Calculator in VB.Net with Mathematical Functions
How to Make a Calculator in VB.Net with Mathematical Functions. This calculator can make all the normal functions of a calculator as wel as some Mathematical functions like Square root, Log,Percentage etc.
In the previous article I show you an example of navigation of records in MYSQL database table in VB.Net.
In this article I am going to show you how you can make a simple calculator in VB.Net with some mathematical functions.
First we design an interface in VB.Net form as shown in the picture.
In the code window of the form write the code as under:
First create two variables f and res as shown under.
Public Class Form1
Dim f As Double
Dim res As Double
Then you need to write the code on each of the buttons click procedure on the VB.Net form.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Text = TextBox1.Text & 1
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox1.Text = TextBox1.Text & 2
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
TextBox1.Text = TextBox1.Text & 3
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
TextBox1.Text = TextBox1.Text & 4
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
TextBox1.Text = TextBox1.Text & 5
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
TextBox1.Text = TextBox1.Text & 6
End Sub
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
TextBox1.Text = TextBox1.Text & 7
End Sub
Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
TextBox1.Text = TextBox1.Text & 8
End Sub
Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
TextBox1.Text = TextBox1.Text & 9
End Sub
Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
TextBox1.Text = TextBox1.Text & 0
End Sub
Private Sub ButtonEqual_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
If f = 1 Then
TextBox1.Text = res + Val(TextBox1.Text)
ElseIf f = 2 Then
TextBox1.Text = res - Val(TextBox1.Text)
ElseIf f = 3 Then
TextBox1.Text = res * Val(TextBox1.Text)
ElseIf f = 4 Then
TextBox1.Text = res / Val(TextBox1.Text)
End If
End Sub
Private Sub btndot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button17.Click
TextBox1.Text = TextBox1.Text & "."
End Sub
Private Sub btndiv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndiv.Click
f = 4
res = Val(TextBox1.Text)
TextBox1.Text = ""
End Sub
Private Sub btnmul_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnmul.Click
f = 3
res = Val(TextBox1.Text)
TextBox1.Text = ""
End Sub
Private Sub btnsub_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsub.Click
f = 2
res = Val(TextBox1.Text)
TextBox1.Text = ""
End Sub
Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click
f = 1
res = Val(TextBox1.Text)
TextBox1.Text = ""
End Sub
Private Sub btnlog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnlog.Click
TextBox1.Text = Math.Log10(TextBox1.Text)
End Sub
Private Sub btnsroot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsroot.Click
TextBox1.Text = Math.Sqrt(TextBox1.Text)
End Sub
Private Sub btnper_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnper.Click
TextBox1.Text = (res * TextBox1.Text) / 100
End Sub
Private Sub btncls_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncls.Click
TextBox1.Text = ""
TextBox1.Focus()
End Sub
There is also a function called Keys which is used to prevent to type any text in the textbox.
Private Function keys(ByVal key As String) As Boolean
If (key >= 48 And key <= 57) Or key = 8 Then
keys = False
Else
keys = True
End If
End Function
Now we can call the function on the keypress event of textbox.
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
e.Handled = keys(Asc(e.KeyChar))
End Sub
Regards
Keep Coding