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
Public 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 Single
        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

        Select 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.

39 comments

when you add 5 +5 +5 the result is 10, which solution you would have?

Reply

For that you need to use & or + to calculate the rest.

Reply

how can i make the calculator to follow BODMAs PRINCIPLE ?

Reply

To be honest with you, I am not good with math! :(

Reply

How to integrate numpad to do the math?

Reply

You can do it with keypress event.

Reply

MOHAMED SHIMRAN Are You There???

Reply

Correction: For the 1-0 Buttons, put:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click, Button4_Click, Button5_Click, Button8_Click, Button9_Click, Button10_Click, Button13_Click, Button14_Click, Button15_Click, Button18_Click


Textbox1.Text = Textbox1.Text & sender.text
End Sub

This can replace some 30 lines of code with 3 lines of code
:D

Reply
This comment has been removed by the author.

Replace the _ with . behind the "Handles" section

Reply

how do you calculate a factorial..what is the code

Reply

Here is a code I found on snipplr

//To calculate a factorial the simplest way and more optimized, you may use the following function

Private Function Factorial(ByVal Num As Int64) As Int64
Factorial = 1
For i = 2 To Num
Factorial *= Num
Next
End Function

//Please be aware that there is an inherent max to all factorial calculations. Based on x86 or x64.
//Let alone the data types being supported. I used a Int64 instead of Integer because Integer is
//actually a wrapper for Int32, which has a smaller min/max

//In some cases, you may want to use:

Private Function Factorial(ByVal Num As UInt64) As UInt64
Dim i As UInt64
Factorial = 1
For i = 2 To Num
Factorial *= Num
Next
End Function

//seeing as the UInt64 allows for a MUCH bigger maximum than the Int64 does. U stands for unsigned,
//meaning its positive numbers only.

Reply

My calculator keeps giving me a zero in front of the number how do I make it disappear. For example 2 + 02

Reply

Hmmm yeah I am getting bad reviews for this.. I will post a new one soon :)

Reply

please give me the link I have a assignment due soon

Reply

Hi,

I couldn't find any better calculators online but maybe you can find some advanced calculator tutorials on YouTube.

Thanks

Reply

How do i implement a third variable for 6+6+6 or any other number

Reply

Mohamed, pls are you there?

Reply

Please, how do i make the calculator in such a way as to display the operation signs??
I mean when i press 2 followed by +, -, /, *... It doesn't display the operation sign, though the answer will be correct when i press the = sign

Reply

I'm planning to improve this but I don't have any time for it. I will update it whenever I can.

Reply

Your calculator sucks ass

Reply

What is the purpose of making such a dumb calculator? Why not complete it ?

Reply

there are too much errors in the codes

Reply

cant understand sir

Reply

this is an organized post this is awesome

Reply

THIS IS FUDGING COOL!
Shut up Anonymous (haters)
JUST DON'T DO IT!

Reply

This calculator does not work. It spews out rubbish number. Please bring this site down. Now. It does not help at all.

Reply

This calculator does not work. It spews out rubbish number. Please bring this site down. Now. It does not help at all.

Reply

Does anyone know the code to change operator

Reply

put your penis in your ass and it should work

Reply

when i paste it it says "hasDecimal is not declared" and here "Dim hasDecimal As Boolean" says identifier expected in the '<'. can you help me please? (Note: i wrote sp@n because it says here in the comment section that sp@n with an a is not allowed, dont know why)

Reply

jesus it doesnt show. this is the code Dim hasDecimal As Boolean

Reply

well, span just doesnt show, its in line 9 of the code, sorry for the confusion

Reply

why in the textbox there is no show 5+5 ???

Reply

Sir please help
cod is full of errors.

Reply

The measure of work you should put compose such an extensive and point by point post is gigantic. can someone do my assignment
I am not that into programming. I know fundamental HTML, CSS and php.
I figure you should begin a YouTube channel too. Every one of the instructional exercises you have on this site are astonishing. I would prescribe your site to my companions in CS/IT
Keep doing awesome

Reply

Post a Comment

Note: Only a member of this blog may post a comment.