How would I go about making a simple calculator
I currently have the buttons set up to display the number selected in the textbox but I cant fiqure outiswhen I click the add button how to get it to store the current numbers in the textboxas a varible then allow more numbers to be added into the text box then add the two or more numbers when I click the equal button.
Thank You!

Caclulator
funkmonkey
Juan Carlos Ruiz Pacheco
rod_r
Hi,
This answer is fairly long because it's mostly code examples. I created a form and series of buttons. This should be fairly self explanatory; however, if you have a question just post a response and I will get back to you to clarify the explanation. The basic idea is that you will use 3 integer variables that will be continuously built-up. The first variable "intCalcStored" will be a storage point for all the calculations, the second "intCalcNew" will be used to temporarily store the new variable from the calculator and later to perform the operation, and the third "intCalcTemp" is used to temporarily hold the new calculations until it is passed back to be stored. There are two public variables; 1) strCalcDisplay, this is simply to store the textbox text; 2) strLastOperation, this is to determine what operation button was pushed last and is used later to perform the calculation. The strCalcDisplay string needs to be converted over to an integer (this is done by using "CInt" conversion). Then there is a boolean variable to be used to determine if the Total ("=") button has been pressed; this is primarily so the end user can continue working off the total (i.e. they decide they would like to subtract a number from the total, etc).
You will find some errors in the methods used for calculations where an end-user could potentially create a calculation that is not accurate, this is because I didn't fully debug it. I wanted to get you a starting position and let you refine the calculations and create coding to handle certain events. Such as end-users repeatedly pressing the total ("=") button and then performing an operation. Now you can also add a "M+", "M-", and "MR" buttons to store values into memory. I would suggest creating a public variable (integer type) called intMemoryStored and then handling the appropriate methods in each button (i.e. "M+" would create an instance of the intMemoryStored, "M-" would clear the variable, and "MR" would recall the value and display it).
In an effort to save some posting space I am only including an instance of one of the numbered command buttons ("1"); the coding is the same for all other buttons with exception of the number that is displayed in the textbox.
Here is the coding:
Public Class frmCalculator
Public intCalcStored As Integer
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadPublic intCalcNew As Integer
Public intCalcTemp As Integer
Public strCalcDisplay As String
Public strLastOperation As String
Public blnTotal As Boolean
ClearCalcs()
blnTotal = False
End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
strCalcDisplay = strCalcDisplay & "1"
TextBox1.Text = strCalcDisplay
End Sub Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
If blnTotal = True Then
strLastOperation = "+"
Exit Sub
End If
intCalcTemp = CalculateSub(TextBox1.Text)
strLastOperation = "+"
TextBox1.Text = ""
End Sub Private Sub cmdTotal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdTotal.Click
intCalcTemp = CalculateSub(TextBox1.Text)
TextBox1.Text = intCalcTemp
blnTotal = True
End Sub Private Sub cmdSubtract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSubtract.Click
If blnTotal = True Then
strLastOperation = "-"
Exit Sub
End If
intCalcTemp = CalculateSub(TextBox1.Text)
strLastOperation = "-"
TextBox1.Text = ""
End Sub Private Sub cmdDivide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDivide.Click
If blnTotal = True Then
strLastOperation = "/"
Exit Sub
End If
intCalcTemp = CalculateSub(TextBox1.Text)
strLastOperation = "/"
TextBox1.Text = ""
End Sub Private Sub cmdMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdMultiply.Click
If blnTotal = True Then
strLastOperation = "*"
Exit Sub
End If
intCalcTemp = CalculateSub(TextBox1.Text)
strLastOperation = "*"
TextBox1.Text = ""
End Sub Private Sub cmdCE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCE.Click
ClearCalcs()
blnTotal = False
End Sub
Public Sub ClearCalcs()
intCalcTemp = 0
intCalcNew = 0
intCalcStored = 0
strCalcDisplay = ""
strLastOperation = ""
TextBox1.Text = ""
blnTotal = False
End Sub Public Function CalculateSub(ByVal intCalc As Integer) As Integer
'Convert textbox string to integer value
intCalcNew = CInt(intCalc)
'Evaluate operation previously performed and perform calculation
'then store calculation for further operations, if requested by user.
If strLastOperation = "" Then
intCalcStored = intCalcNew
ElseIf strLastOperation = "+" Then
intCalcStored = intCalcStored + intCalcNew
ElseIf strLastOperation = "-" Then
intCalcStored = intCalcStored - intCalcNew
ElseIf strLastOperation = "/" Then
intCalcStored = intCalcStored / intCalcNew
ElseIf strLastOperation = "*" Then
intCalcStored = intCalcStored * intCalcNew
Else
MessageBox.Show("Operation was not detected properly. Please retry", "Missing Operation")
End If
strCalcDisplay = ""
Return intCalcStored
End Function
End
ClassI hope you find this helpful. Please feel free to ask about any part of the coding that doesn't make sense.
Thank you,
James
White Hawk
There's no such a thing as "long double integer", but perhaps the Decimal type in VB can suite your needs. It's larger than long, double or integer.
HTH