Hello-
I am trying to write the code for a basic calculator in VB using functions but when I click on the numbers when i run it, they do not appear in the display text (txtResult.text). Here is what I have. Thanks! Jackie
Dim oper As String
Dim myVar As Double
Dim myVar2 As Double
Option Explicit
'CALCULATOR TAB
Private Sub cmdEquals_Click()
Dim result As Double
myVar = txtResult.Text
txtResult.Text = ""
'CALL Calculator
result = Calculator(myVar, myVar2, oper)
txtResult.Text = result
If Val(txtResult.Text) < 0 Then
txtResult.ForeColor = vbRed
Else: txtResult.ForeColor = vbBlack
End If
If Val(txtResult.Text) >= 0 Then
txtResult.ForeColor = vbBlack
Else: txtResult.ForeColor = vbRed
End If
cmdRealDivide.BackColor = vbRed
cmdPlus.BackColor = vbRed
cmdSubtract.BackColor = vbRed
cmdMultiply.BackColor = vbRed
cmdIntegerDivision.BackColor = vbRed
cmdModulus.BackColor = vbRed
cmdExponent.BackColor = vbRed
cmdSquareRoot.BackColor = vbRed
cmdSquared.BackColor = vbRed
cmdNegPos.BackColor = vbRed
End Sub
Private Function Calculator(a As Double, b As Double, c As String) As Double
Dim result As Double 'function to calculate events
If c = "+" Then
result = a + b
ElseIf c = "/" Then
result = a / b
calc = result
ElseIf c = "-" Then
result = a - b
calc = result
ElseIf c = "*" Then
result = a * b
calc = result
ElseIf c = "\" Then
result = a \ b
calc = result
ElseIf c = "M" Then
result = a Mod b
calc = result
ElseIf c = "x^y" Then
result = a ^ b
End If
End Function
I'm assuming that you have other buttons to populate the numbers? For example a button for the "1" that has:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.txtResult.AppendText("1
End Sub
Cheers!