Visual basic simple mathematical calculator

Posted in: Visual Basic by musiitwa joseph 2015-06-27 at 06:55:17

image for Visual basic simple mathematical calculator The calculator shows the use of mathematical operators(addition, subtraction, division and multiplication) with two operands. The user enters the two numbers into the text boxes. When he/she clicks one of the buttons (+, - , /, *), the input is checked whether its numeric using the isnumeric function. if true calculation is performed.

The data types of the numbers are capture as double to cater for both integers and decimal numbers.
When dividing there is a possibility of the user dividing a number by zero, this results into a division by zero error which is handled by using the IsNegativeInfinity and IsPositiveInfinity.

Public Class Form1
Dim number1, number2, answer As Double

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

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

If IsNumeric(TextBox1.Text) And IsNumeric(TextBox2.Text) Then
number1 = TextBox1.Text
number2 = TextBox2.Text
answer = number1 + number2
Label3.Text = answer.ToString
Else
Label3.Text = "Enter numbers only"
End If

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If IsNumeric(TextBox1.Text) And IsNumeric(TextBox2.Text) Then
number1 = TextBox1.Text
number2 = TextBox2.Text
answer = number1 - number2
Label3.Text = answer.ToString
Else
Label3.Text = "Enter numbers only"
End If
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If IsNumeric(TextBox1.Text) And IsNumeric(TextBox2.Text) Then
number1 = TextBox1.Text
number2 = TextBox2.Text
answer = number1 * number2
Label3.Text = answer.ToString
Else
Label3.Text = "Enter numbers only"
End If
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
If IsNumeric(TextBox1.Text) And IsNumeric(TextBox2.Text) Then
number1 = TextBox1.Text
number2 = TextBox2.Text
answer = number1 / number2
If Double.IsNegativeInfinity(answer) Or Double.IsPositiveInfinity(answer) Then
Label3.Text = "Division by zero"
Else
Label3.Text = answer.ToString
End If

Else
Label3.Text = "Enter numbers only"
End If
End Sub
End Class



Search

Leave a comment

Fields with * are required
Your email address will not be displayed
5 + 1 =