0

I already made the calculator, but I'm trying to also put the function used in the textbox results. Example:

2 * 2 = The function used was multiplication. Your answer is 4

I used labels to identify -, +, *, /
Two textboxes to put in input.
1 textbox for ouput.

I used this sample code for my calculator :

lblnum1 = "+"
lblnum1 = val(txta.text) + val(txtb.text)


the output i want is:
2 * 2 = The function used was multiplication. Your answer is 4

3
Contributors
6
Replies
7
Views
9 Years
Discussion Span
Last Post by Prozeen
0

Here's a program to look at. My apologies for not using text boxes. I used buttons and then realized my mistake after re-reading your post. Still, the same theory applies... Store the values, and then include those values in your answer.

Attachments
0

Ohhh nice one :D. Thanks, but i used visual basic 2005 express and i cant open the .vb files or which ever ones for the code ? how can i view the code ?

0

In whatever code you develop to solve this problem, consider what you would do if the user types in a formula that contains multiple functions (e.g. 3 + 4 - 2).

Hoppy

0

Glad you liked it. Here's the code. You may need to do some changes to it, because of the software you're using. Try the vb.net forum if you get stuck. Good luck :)

Attachments
Dim status
Dim num1
Dim num2
Dim operinfo

Private Sub cmdequals_Click()

'Work out which function/operator to use.
'Do the calculation.
If operinfo = "addition" Then
answer = num1 + num2
ElseIf operinfo = "subtraction" Then
answer = num1 - num2
ElseIf operinfo = "multiplication" Then
answer = num1 * num2
ElseIf operinfo = "division" Then
answer = num1 / num2
End If

'Show the answer and other information.
lblanswer.Caption = _
"The first number was " & num1 & "." & Chr(13) _
& "The second number was " & num2 & "." _
& Chr(13) & Chr(13) & "The function used was " & operinfo & "." _
& Chr(13) & Chr(13) & "Your answer is " & answer & "."

'Disable the Equals button.
cmdequals.Enabled = False

End Sub

Private Sub cmdexit_Click()

'Exit the program.
End

End Sub

Private Sub cmdinteger_Click(Index As Integer)

'Clear the intitial caption.
lblanswer.Caption = ""

'Store the number, based on the status of the program.
'Change the status of the program.
'Disable or show the relevant buttons.
If status = 1 Then
    If Index = 0 Then
    num1 = 1
    ElseIf Index = 1 Then
    num1 = 2
    End If
    For b = 0 To 3
    lbloperate(b).Enabled = True
    lbloperate(b).BackColor = &HC0FFC0 'Green
    Next b
status = 2
cmdinteger(0).Enabled = False
cmdinteger(1).Enabled = False
Exit Sub
ElseIf status = 2 Then
    If Index = 0 Then
    num2 = 1
    ElseIf Index = 1 Then
    num2 = 2
    End If
cmdequals.Visible = True
cmdinteger(0).Enabled = False
cmdinteger(1).Enabled = False
Exit Sub
End If

'Enable the 4 function/operator labels.
For a = 0 To 3
lbloperate(a).Enabled = True
Next a

End Sub

Private Sub Form_Load()

'Set the initial caption.
lblanswer.Caption = Chr(13) & Chr(13) & "Please click on a number."

'Set the initial status of the program.
status = 1

End Sub

Private Sub lbloperate_Click(Index As Integer)

'Enable the integer buttons.
cmdinteger(0).Enabled = True
cmdinteger(1).Enabled = True

'Store the function chosen by the user.
If Index = 0 Then
operinfo = "addition"
ElseIf Index = 1 Then
operinfo = "subtraction"
ElseIf Index = 2 Then
operinfo = "multiplication"
ElseIf Index = 3 Then
operinfo = "division"
End If

'Disable the 4 function labels.
'Change their color.
For b = 0 To 3
lbloperate(b).Enabled = False
lbloperate(b).BackColor = &H8080FF 'Red.
Next b

End Sub
0

Thanks Prozeen. I got it working :D. Code helped a lot thanks :D.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.