Solved

Simple calculator using functions (Visual Basic 6.0)

Posted on 2006-11-19
10
507 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:jlx2
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 16

Expert Comment

by:Hillwaaa
ID: 17976221
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!
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 17976227
"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?...
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 17976229
By the way, you posted your question in the VB.Net area, not the VB6 area:
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/

0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 44

Expert Comment

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

AW
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 17976301
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
0
 
LVL 16

Expert Comment

by:Hillwaaa
ID: 17976340
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!
0
 

Author Comment

by:jlx2
ID: 17976364
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.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 17976437
So the code for the "1" would probably look something like:

    Private Sub cmdOne_Click()
        txtResult.Text = txtResult.Text & "1"
    End Sub
0
 
LVL 44

Accepted Solution

by:
Arthur_Wood earned 50 total points
ID: 17976681
actually, you need three text boxes on your form:

txtVar1  The value on the LEFT SIDE of the operator
txtVar2  The value on the RIGHT SIDE of the Operator
txtResult  The value that is the ruesult of the calculation

LEFT_SIDE  operator RIGHT_SIDE = RESULT

The your number buttons would have code like this:


Private Sub cmdOne_CLick()
    SetValue txtOper, 1
End Sub


Private Sub cmdTwo_CLick()
    SetValue txtOper, 2
End Sub

and so on for 3 through 9, and 0

Private Sub cmdPlus_Click()
    txtOper = "+"
End Sub

Private Sub cmdMinus_Click()
    txtOper = "-"
End Sub

Private Sub cmdTimes_Click()
    txtOper = "*"
End Sub

Private Sub cmdDivide_Click()
    txtOper = "/"
End Sub

Private Sub cmdIntDivide_Click()
    txtOper = "\"
End Sub

Private Sub cmdMod_Click()
    txtOper = "M"
End Sub

Private Sub cmdPower_Click()
    txtOper = "^"
End Sub

Private Sub SetValue(Operator as String, Value as String)
    If Operator = "" then
        TxtLeftSide = txtLedtSide & Value
    Else
        txtRightSide = txtRightSide & Value
   End If
End Sub

then in cmdEquals (which will do the calculation):

Private Sub cmdEquals_Click()
    Dim oper as String
    Dim val1 as Integer
    Dim val2 as Integer
    Dim Result as Double

    oper = txtOper.Text
    txtOper.Text = ""
    val1 = Val(txtLeftSide.Text)
    val2 = val(txtRightSide.Text)
   
    Result = Calculator(val1, val2, oper)  ' use your function from the original question

    txtResult = Format(Result, "#.0000")

End Sub

AW

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 17977024
"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...
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Exit the loop 4 49
Disable extension 8 48
Help with exporting to excel 4 36
Access/Visual Basic Question 3 26
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question