Saturday, February 23, 2013

Simple Calculator Using VB 2008

Simple calculator using VB.


Here I show you how to make a simple calculator which can do basic mathematical operations like addition, subtraction,  multiplication and division.


I used Visual Basic 2008 for this. The same can be used with  VB 2006 also.

See the Picture below to know the Command Button I used for the calculator


Command Button 1 to 10  are the numbers from 1 to 9 and 0 respectively.
Command Button 11 is the Decimal point.
Command Button 12 is the Clear Button. It clear the display to blank.
Command Button 13 to 16 are Addition , Subtraction, Multiplication, Division respectively.
Command Button 17 is the equals to Button. It displays the result.
Command Button 18 is the backspace button. Once pressed it removes the last letter added on the right    hand side. It helps correct the number in case of any wrong entry.

 see the screen shot of the window showing the codes.


Given Below is the Codes for the Calculator.




Public Class Form1
    Dim a As String
    Dim b As String
    Dim c As String
    Dim d As Integer
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Button17.Text = "="

    End Sub

    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 Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        TextBox1.Text = TextBox1.Text & 5
    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 Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        TextBox1.Text = TextBox1.Text & 6
    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 Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        TextBox1.Text = TextBox1.Text & 4
    End Sub

    Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
        If a = 0 Then
            TextBox1.Text = TextBox1.Text & "."
            a = 1
        Else
            TextBox1.Text = TextBox1.Text & ""
        End If

    End Sub

    Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
        TextBox1.Text = ""
        a = 0
        b = 0
        d = 0
        Label1.Text = "Enter First Number"

    End Sub

    Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
        b = TextBox1.Text
        c = 1
        TextBox1.Text = ""
        a = 0
        Label1.Text = "Enter Next Number"
    End Sub

    Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Click
        b = TextBox1.Text
        c = 2
        TextBox1.Text = ""
        a = 0
        Label1.Text = "Enter first Number"
    End Sub

    Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click
        b = TextBox1.Text
        c = 3
        TextBox1.Text = ""
        a = 0
        Label1.Text = "Enter first Number"
    End Sub

    Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click
        b = TextBox1.Text
        c = 4
        TextBox1.Text = ""
        a = 0
        Label1.Text = "Enter first Number"
    End Sub

    Private Sub Button17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button17.Click
        d = TextBox1.Text
        If c = 1 Then
            TextBox1.Text = Val(b + d)
            c = 0
            a = 0
            b = 0
            d = 0
            Label1.Text = "Press C to Clear"
        ElseIf c = 2 Then
            TextBox1.Text = Val(b - d)
            c = 0
            a = 0
            b = 0
            d = 0
        ElseIf c = 3 Then
            TextBox1.Text = Val(b * d)
            c = 0
            a = 0
            b = 0
            d = 0
        ElseIf c = 4 Then
            TextBox1.Text = Val(b / d)
            c = 0
            a = 0
            b = 0
            d = 0
        Else : TextBox1.Text = TextBox1.Text
        End If
    End Sub

 
    Private Sub Button18_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button18.Click
        If Len(TextBox1.Text) = 0 Then Exit Sub
        TextBox1.Text = Mid(TextBox1.Text, 1, Len(TextBox1.Text) - 1)
    End Sub
End Class

No comments:

Post a Comment