Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
this is my calculator design:

https://www3.0zz0.com/2020/05/28/19/797035332.jpg[^]

the upper textbox is for calculation while the lower is for result
it is very simple for me to write a calculator which can do simple process like:
1 + 1 or 2 -1
but i want a calculator like windows calculator
which can do different processes at once like:

https://www5.0zz0.com/2020/05/28/19/531418581.png[^]

for example, if i write 1 + 2 then pressed =, the lower textbox show the result of 3
the same must happen when i press +, the lower textbox show 3 and upper textbox add
+ to the calculation and so on.

What I have tried:

i managed to solve the addition (+) function well
but the other 3 processes isn't working
the subtraction (-) process do addition with - in front of the number
example: 8 - 2 result is -10
multiply and divide processes always result 0
this is my code:

VB
Public Class Form1

    Private Sub buttons(sender As Object, e As EventArgs) Handles Btn0.Click, Btn1.Click, Btn2.Click, Btn3.Click, Btn4.Click, Btn5.Click, Btn6.Click, Btn7.Click, Btn8.Click, Btn9.Click

        If CalculatorText.Text = "" Then
            CalculatorText.Text = sender.text
        Else CalculatorText.Text = CalculatorText.Text & sender.text
        End If

    End Sub
    Private Sub BtnPoint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnPoint.Click

        CalculatorText.Text += BtnPoint.Text

    End Sub
    Private Sub BtnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClear.Click

        CalculatorText.Clear()
        CalculatorResult.Clear()

    End Sub
    Private Sub BtnDelete_Click(sender As Object, e As EventArgs) Handles BtnDelete.Click
        CalculatorText.Text = CalculatorText.Text.Remove(CalculatorText.Text.Length - 1, 1)
    End Sub
    Private Sub BtnPlus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnPlus.Click

        Dim plus As String()
        Dim num As Long

        If CalculatorText.Text.Contains("+") Then
            plus = CalculatorText.Text.Split("+")
            For Each NumToPower As String In plus
                num += CStr(NumToPower)
            Next
            CalculatorResult.Text = "= " & num
            CalculatorText.Text += "+"
        Else
            CalculatorText.Text += "+"
        End If

    End Sub
    Private Sub BtnMinus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnMinus.Click
        CalculatorText.Text += "-"
    End Sub
    Private Sub BtnDivide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDivide.Click
        CalculatorText.Text += "/"
    End Sub
    Private Sub BtnMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnMultiply.Click
        CalculatorText.Text += "*"
    End Sub
    Private Sub CalculatorText_TextChanged(sender As Object, e As EventArgs) Handles CalculatorText.TextChanged

        If CalculatorText.Text = "" Then
            BtnResult.Enabled = False
            BtnPlus.Enabled = False
            BtnMinus.Enabled = False
            BtnMultiply.Enabled = False
            BtnDivide.Enabled = False
        End If

        If CalculatorText.Text <> "" Then

            Dim s As String = CalculatorText.Text.Substring(Len(CalculatorText.Text) - 1, 1)
            If s = "+" Or s = "-" Or s = "/" Or s = "*" Then
                BtnResult.Enabled = False
                BtnPlus.Enabled = False
                BtnMinus.Enabled = False
                BtnMultiply.Enabled = False
                BtnDivide.Enabled = False
            End If
            If s <> "+" And s <> "-" And s <> "/" And s <> "*" Then
                BtnResult.Enabled = True
                BtnPlus.Enabled = True
                BtnMinus.Enabled = True
                BtnMultiply.Enabled = True
                BtnDivide.Enabled = True
            End If

        End If
    End Sub
    Private Sub BtnResult_Click(sender As Object, e As EventArgs) Handles BtnResult.Click

        Dim result As Double
        Dim plus As String()
        Dim Substract As String()
        Dim multiply As String()
        Dim divide As String()
        Dim num As Long

        If CalculatorText.Text.Contains("+") Then
            plus = CalculatorText.Text.Split("+")
            For Each NumToPower As String In plus
                num += CStr(NumToPower)
            Next
        End If

        If CalculatorText.Text.Contains("-") Then
            Substract = CalculatorText.Text.Split("-")
            For Each NumToPower As String In Substract
                num -= CStr(NumToPower)
            Next
        End If

        If CalculatorText.Text.Contains("/") Then
            divide = CalculatorText.Text.Split("/")
            For Each numbertodivide As String In divide
                If numbertodivide <> 0 Then
                    num /= CStr(numbertodivide)
                    result = num
                Else
                    MessageBox.Show("Cannot Divide By 0")
                    Exit Sub
                End If
            Next
        End If

        If CalculatorText.Text.Contains("*") Then
            multiply = CalculatorText.Text.Split("*")
            For Each NumToPower As String In multiply
                num *= CStr(NumToPower)
            Next
        End If

        CalculatorResult.Text = "= " & num

    End Sub

End Class
Posted
Updated 28-May-20 8:11am
v3
Comments
CHill60 28-May-20 13:15pm    
"isn't working" doesn't help us to help you - what happens or doesn't happen
Doctor GME 28-May-20 13:54pm    
sorry for this
the question has been edited...

1 solution

Your concept of how to parse the calculator text seems a little strange
VB
If CalculatorText.Text.Contains("-") Then
    Substract = CalculatorText.Text.Split("-")
    For Each NumToPower As String In Substract
        num -= CStr(NumToPower)
    Next
End If
Firstly, NumToPower is already a String so there is no need for Cstr(NumToPower). CStr is very old-school anyway - if you really do want to convert things to strings use Convert.ToString() instead.

You are trying to add a string to a long - that is bad practice, you might get away with it sometimes in VB but not always. You need to convert NumToPower to a Long.
Use Long.Parse or better, Long.TryParse. Here's the reference information Parsing Numeric Strings in .NET | Microsoft Docs[^]

Now step through what actually happens, either on paper or in your debugger.

As you hit this block of code num = 0, NumToPower is going to get the value 8 first, so your code says
VB
num -= 8
which equals -8
On the next pass, NumToPower = -2 so
VB
num -=2
is the same as
-8-2
which equals -10

Basically, your logic of how to apply the operators to the numbers is incorrect.

Also consider the calculator text
8 - 2 + 4
Your logic will first split that on the + sign and you will get two items in the array "8 - 2" and "4" - when you try to convert that first one to a numeric value you are going to get an error.

You need to rethink how to parse the calculator string to get the numbers and operators out of it I'm afraid

Edit - I found a couple of helpful articles here on CodeProject

Here is a simple example that you could emulate ..
A Simple Calculator in Windows Forms[^]

Or if you want to do a more complicated level of parsing the entire input string have a look at
Parsing Mathematical Expressions in VB.NET: Mission Possible[^]
 
Share this answer
 
v3
Comments
phil.o 28-May-20 14:23pm    
5'd
Maciej Los 28-May-20 15:07pm    
Agree!
Richard Deeming 28-May-20 15:05pm    
The big difference between CStr(x) and x.ToString() is that CStr won't throw a NullReferenceException if the object happens to be null / Nothing. :)

Convert.ToString might be a better replacement.
CHill60 29-May-20 4:32am    
Good point - I've updated my solution!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900