Ask Experts Questions for FREE Help !
Ask
    neo_lover2000's Avatar
    neo_lover2000 Posts: 12, Reputation: 1
    New Member
     
    #1

    May 28, 2009, 08:41 PM
    visual basic
    I already finish a very simple calculator,actually it has only four buttons.
    at first its working fine but when add another feature its not working anymore. What I want to do is when press 1 then press + then press 1 and press + again it should add the first two number get ready to add another number. I don't know how to work it out I'm new vb6
    need somebody to revise my program.
    thanks

    Dim first As Long
    Dim second As Long
    Dim sign As String
    Dim Calculated As Boolean

    Private Sub Command1_Click()

    TextDisplay.Text = TextDisplay.Text + Command1.Caption

    End Sub

    Private Sub cmdPlus_Click()
    Calculated = True

    If Calculated = True Then
    first = Val(TextDisplay.Text)
    TextDisplay.Text = ""
    second = Val(TextDisplay.Text)
    first = first + second
    TextDisplay.Text = first
    second = 0

    Else
    first = Val(TextDisplay.Text)
    TextNum1.Text = first
    TextDisplay.Text = ""
    sign = cmdPlus.Caption

    End If
    End Sub

    Private Sub cmdC_Click()
    TextDisplay.Text = ""
    TextNum1.Text = ""
    TextNum2.Text = ""
    TextFlag.Text = ""
    End Sub

    Private Sub cmdEq_Click()
    second = Val(TextDisplay.Text)
    TextNum2.Text = second
    If sign = "+" Then
    TextDisplay = first + second
    End If
    End Sub
    Perito's Avatar
    Perito Posts: 3,139, Reputation: 150
    Ultra Member
     
    #2

    May 29, 2009, 08:00 AM
    I guess you're only adding integers since your "first" and "second" variables are longs.

    I think that this is what you want to do. If it's not quite what you need, post back.

    Private Sub cmdPlus_Click()
    if (something) then ' This has to flag the condition that he has just entered a number and hit "+". There has to be something left empty to show that he wants to use the last entered value as an operand. This is the fundamental problem in the code.
    Calculated = False
    else
    Calculated = True
    end if

    If Calculated = True Then ' As originally written, this Calculated will always be True.
    ' I assume that this part works.
    first = Val(TextDisplay.Text)
    TextDisplay.Text = ""
    second = Val(TextDisplay.Text)
    first = first + second
    second = 0
    TextDisplay.Text = first
    second = 0

    Else

    first = Val(TextDisplay.Text) ' This is the value on the screen.
    second = val(TextNum1.Text) ' Pick up the value that he just entered
    TextNum1.Text = first
    first = first + second
    TextDisplay.Text = first
    sign = cmdPlus.Caption ' I'm not sure what you're using this for.

    ' I think you wanted this subroutine to be called automatically and you'd use the "Calculated" variable to steer the program flow. There are two problems with your approach. 1) there's nothing to trigger the event (you could deliberately call this subroutine again), and 2) Calculated is a local variable, not a global variable. A new variable, Calculated is created and set each time this subroutine is called. I suggest you just calculate the value you want in the "else" section of the program. If the user hasn't entered a variable, jump to the else section.
    End If
    End Sub

    In most calculators, I believe they remember the second number you typed in. The Windows calculator remembers the last calculated value. It doesn't display either one. You could just put it in a variable that persists from one operation to the next, and never show it on the screen. Alternatively, you could put it in a textbox that is disabled (textboxname.enabled = false). It would be grayed out, but the user could still see what's saved. In the above, you would select "first" from the box where he entered a value and "second" from this grayed out box (or the hidden variable). If "C"lear is clicked, be sure to zero the saved textbox or the variable.

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

Visual basic 6 [ 1 Answers ]

Hi! I am facing difficulty in preparing SRS(software requirement specification) of my semister project which is Credit Card System(visual basic 6) please give me some suggesion. Thank you

Visual Basic [ 1 Answers ]

Actually I'm a new user(student) of Visual Basic.So I want to know how I am make a simple calculator in Visual Basic 6?

Visual Basic 6.0 [ 1 Answers ]

What is the cost of Visual Studio 6.0 Enterprise Edition?


View more questions Search