I made a calculator using vb.net. 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
Else
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
Else
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.