How To Make A Advance Calculator In VB.NET

Today we are going to make an advanced calculator(no), this is how our calculator looks like after designing it.

To begin open your visual basic 2008/2010 and make a new project and name it whatever you want and go to your form properties and change size to 258, 269 also change the form text .
Now add 2 group boxes and re-size them as below

GroupBox1 - 218, 43
GroupBox2 - 222, 161
After that add a textbox and time to add the buttons
Create 2 buttons and change the text of them as given below and the buttons sorted by 1-22
Button1 - Back Space
Button2 - C
Button3 - 1
Button4 - 2
Button5 - 3
Button6 - /
Button7 - x^
Button8 - 4
Button9 - 5
Button10 - 6
Button11 - *
Button12 - Sqrt
Button13 - 7
Button14 - 8
Button15 - 9
Button16 - -
Button17 - 1/x
Button18 - 0
Button19 - +/-
Button20 - .
Button21 - +
Button22 - =
Now make sure you sorted the buttons 1-22 and the order is correct like the picture of the application given below
now all you have to do to make the program is just time to code now i dont want to make you get bored so just add this all the codes
Option Explicit On
<span class="IL_AD" id="IL_AD7">Public</span> Class Form1

    'Author : Mohamed Shimran
    'Blog : http://www.ultimateprogrammingtutorials.blogspot.com

    Dim Operand1 As Double
    Dim Operand2 As Double
    Dim [Operator] As String
    Dim hasDecimal As Boolean
    Dim tmpValue As Double
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Declare locals needed
        Dim str As String
        Dim loc As Integer
        'Make sure the text length is > 1
        If Textbox1.Text.Length > 0 Then
            'Get the next to last character
            str = Textbox1.Text.Chars(Textbox1.Text.Length - 1)
            'Check if its a decimal
            If str = "." Then
                'If it is toggle the hasDecimal flag
            End If
            'Get the length of the string
            loc = Textbox1.Text.Length
            'Remove the last character, incrementing by 1
            Textbox1.Text = Textbox1.Text.Remove(loc - 1, 1)
        End If
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Textbox1.Text = ""
    End Sub
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Textbox1.Text = Textbox1.Text & sender.text
    End Sub
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Textbox1.Text = Textbox1.Text & sender.text
    End Sub
    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Textbox1.Text = Textbox1.Text & sender.text
    End Sub
    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        Textbox1.Text = Textbox1.Text & sender.text
    End Sub
    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
        Textbox1.Text = Textbox1.Text & sender.text
    End Sub
    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        Textbox1.Text = Textbox1.Text & sender.text
    End Sub
    Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
        Textbox1.Text = Textbox1.Text & sender.text
    End Sub
    Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Click
        Textbox1.Text = Textbox1.Text & sender.text
    End Sub
    Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click
        Textbox1.Text = Textbox1.Text & sender.text
    End Sub
    Private Sub Button18_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button18.Click
        Textbox1.Text = Textbox1.Text & sender.text
    End Sub
    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Operand1 = Val(Textbox1.Text)
        Textbox1.Text = ""
        Textbox1.Focus()
        [Operator] = "/"
    End Sub
    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        Operand1 = Val(Textbox1.Text)
        Textbox1.Text = ""
        Textbox1.Focus()
        [Operator] = "^"
    End Sub
    Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
        Operand1 = Val(Textbox1.Text)
        Textbox1.Text = ""
        Textbox1.Focus()
        [Operator] = "*"
    End Sub
    Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
        'Make sure the input box has a value
        If Textbox1.Text.Length <> 0 Then
            'Assign our variable the value in the input box
            tmpValue = CType(Textbox1.Text, Double)
            'Perform the square root
            tmpValue = System.Math.Sqrt(tmpValue)
            'Display the results in the input box
            Textbox1.Text = CType(tmpValue, String)
            'Clear the decimal flag
            hasDecimal = False
        End If
    End Sub
    Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click
        Operand1 = Val(Textbox1.Text)
        Textbox1.Text = ""
        Textbox1.Focus()
        [Operator] = "-"
    End Sub
    Private Sub Button17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button17.Click
        Dim convert As <span class="IL_AD" id="IL_AD4">Single</span>
        If Textbox1.Text <> 0 Then
            convert = 1 / Val(Textbox1.Text)
            Textbox1.Text = convert
        End If
    End Sub
    Private Sub Button19_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button19.Click
        Textbox1.Text = -1 * Textbox1.Text
    End Sub
    Private Sub Button20_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button20.Click
        If InStr(Textbox1.Text, ".") > 0 Then
            Exit Sub
        Else
            Textbox1.Text = Textbox1.Text & "."
        End If
    End Sub
    Private Sub Button21_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button21.Click
        Operand1 = Val(Textbox1.Text)
        Textbox1.Text = ""
        Textbox1.Focus()
        [Operator] = "+"
    End Sub
    Private Sub Button22_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button22.Click
        Dim Result As Double
        Operand2 = Val(Textbox1.Text)

        'If [Operator] = "+" Then
        '    Result = Operand1 + Operand2
        'ElseIf [Operator] = "-" Then
        '    Result = Operand1 - Operand2
        'ElseIf [Operator] = "/" Then
        '    Result = Operand1 / Operand2
        'ElseIf [Operator] = "*" Then
        '    Result = Operand1 * Operand2
        'End If

        <span class="IL_AD" id="IL_AD9">Select</span> Case [Operator]
            Case "+"
                Result = Operand1 + Operand2
                Textbox1.Text = Result.ToString()
            Case "-"
                Result = Operand1 - Operand2
                Textbox1.Text = Result.ToString()
            Case "/"
                Result = Operand1 / Operand2
                Textbox1.Text = Result.ToString()
            Case "*"
                Result = Operand1 * Operand2
                Textbox1.Text = Result.ToString()
            Case "^"
                Result = Operand1 ^ Operand2
                Textbox1.Text = Result.ToString()
            Case "%"
                Result = Operand1 * 1 / 100
                Textbox1.Text = Result.ToString()
        End Select
        Textbox1.Text = Result.ToString()
    End Sub
End Class


now debug and enjoy , comment if there are any questions.

Share this

Related Posts

Previous
Next Post »