Link to home
Start Free TrialLog in
Avatar of jlx2
jlx2

asked on

Simple calculator using functions (Visual Basic 6.0)

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
Avatar of Hillwaaa
Hillwaaa
Flag of Australia image

Hi jlx2,

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!
Avatar of Mike Tomlinson
"but when I click on the numbers when i run it, they do not appear in the display text (txtResult.text)"

I don't see any code there to handle the clicking of the "number" buttons...

Did you not post that part?

What are the names of the "number" buttons?  You need to add Click() handlers that add that buttons number to the display right?...
By the way, you posted your question in the VB.Net area, not the VB6 area:
https://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/

Since this is the VB.Net section, that is why Hillwaaa's answer above may look a bit strange to you.

AW
in this section:

    ElseIf c = "/" Then
    result = a / b
    calc = result


What will you do it b = 0 ?

same goes for

    ElseIf c = "\" Then
    result = a \ b
    calc = result

AW
Yeah, I wondered why the code looked a bit strange to me :)

Still I think the point that Idle_mind and I made is valid - if the problem is that clicking the number buttons does not put the numbers into the textbox, then you need to add some code to handle the button clicks.

Good spot on the divide by zero too!
Avatar of jlx2
jlx2

ASKER

Thank-you for your responses.  Apparently I do not have anything set up for when I click on a number.  Each button is a command (i.e., cmdOne, cmdPlu, cmdEquals, etc).  This assignment was originally done the long way so I had a code under each separate command button.  Now we are in procedures/functions and I am not sure how to make the clicks appear in the display.

Thanks-
Jackie

P.S.  sorry i posted in VB.Net; I did not see the VB listed when I asked the quesiton.
So the code for the "1" would probably look something like:

    Private Sub cmdOne_Click()
        txtResult.Text = txtResult.Text & "1"
    End Sub
ASKER CERTIFIED SOLUTION
Avatar of Arthur_Wood
Arthur_Wood
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
"actually, you need three text boxes on your form"

You don't NEED three TextBoxes...a calculator only has ONE display right?  =)

When an operand is clicked you store the current value in the display in a variable for later use...