Click here to Skip to main content
15,664,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I made a calculator using What I want to happen is to make the calculator calculate every time an operation is pressed without pressing the equals sign. I don't know how to do it.

This is my code Im using for my 0-9 buttons:
Private Sub btnNumbers_Click(sender As Object, e As EventArgs) Handles btn9.Click, btn8.Click, btn6.Click, btn5.Click, btn4.Click, btn3.Click, btn2.Click, btn1.Click, btn0.Click

       Dim btnNum As Button = CType(sender, Button)

       input = CDbl(btnNum.Text)

       If txtInput.Text = "0" Or flag = True Then

           txtInput.Text = ""
           txtInput.Text = btnNum.Text
           flag = False


           txtInput.Text = txtInput.Text & btnNum.Text

       End If

   End Sub

This is the code I'm using for my operators buttons:

Private Sub btnSign(sender As Object, e As EventArgs) Handles btnMultiply.Click, btnMinus.Click, btnDivide.Click, btnAdd.Click

        Dim btnSigns As Button = CType(sender, Button)
        operation = btnSigns.Text

        If txtInput.Text <> 0 Then

                operation = btnSigns.Text
                txtHistory.Text = txtHistory.Text & " " & txtInput.Text & " " & btnSigns.Text
                flag = True


                operation = btnSigns.Text
                txtHistory.Text = txtHistory.Text & " " & txtInput.Text & " " & btnSigns.Text
                flag = True
            End If

    End Sub

What I have tried:

My solution is like this:
1.user input a number on txtInput.text then save it to variable 1.
2.if the user presses an operator and input another number in txtInput.text that
number will be saved to variable2 since variable1 has already a number in it.
3.if the user presses an operator again do the calculation.

I'm keeping track of the operator that has been pressed so that the program will know if it needs to add,subtract, etc.
What I'm struggling with, is how can I keep track of the variable2? because I can track the variable1 obviously.

I don't know what event should I trigger to keep track of the variable2.
Updated 31-May-20 19:54pm
Patrice T 1-Jun-20 0:21am    
What picture ?
lelouch_vi 2 1-Jun-20 1:33am    
sorry I thought I can attach picture here while I'm typing my post. Forgot to remove it.

You can't really do that unless you keep a record of the whole calculation so you can "recalculate" the whole expression on the fly - think about it:
I enter "1 + 2"
You calculate "1 + 2 equals 3"
I press "* 3"
You calculate "3 * 3" equals 9
But what I wanted was 1 + 2 * 3 equals 1 + 6 equals 7.

So what I'd suggest is keeping the whole calculation and processing the whole thing each time the user enters something if you create three classes:
An abstract CalculationItem, a Operand (derived from CalculationItem), and an Operator a Operand (derived from CalculationItem), then create a List of CalculationItems to store them in - you can then process that collection in many ways to get the "right" result.
Share this answer
You can use shared variables to keep the value, see:[^]
Share this answer

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