7 Replies - 2833 Views - Last Post: 09 May 2012 - 10:29 AM Rate Topic: -----

#1 vera_chocolate   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 08-May 12

calculator in vb6 couple questions

Posted 08 May 2012 - 06:47 AM

hello, I am new in visual basics, so i decide to create a calculator application, but i faced a couple problem.
1) I didn't know how to create percentage button
2) after it gives result, i want it to clear the result with pressing any number button.
Please help me to find out the solution!
Thank you for your time!
Option Explicit
   Dim UpToDateCalculation As Double
   Dim Times As Boolean
   Dim Add As Boolean
   Dim TakeAway As Boolean
   Dim Divide As Boolean
   Dim Equals As Boolean
 
   


  
Private Sub cmd0_Click(Index As Integer)
If (txtDisplay.Text = 0) Then
    txtDisplay.Text = "0"
     Else
      txtDisplay.Text = txtDisplay.Text & "0"
  End If
End Sub

Private Sub cmd1_Click(Index As Integer)
If (txtDisplay.Text = 0) Then
    txtDisplay.Text = "1"
     Else
      txtDisplay.Text = txtDisplay.Text & "1"
End If

End Sub

Private Sub cmd2_Click(Index As Integer)
If (txtDisplay.Text = 0) Then
    txtDisplay.Text = "2"
     Else
      txtDisplay.Text = txtDisplay.Text & "2"
      End If
End Sub

Private Sub cmd3_Click(Index As Integer)
If (txtDisplay.Text = 0) Then
    txtDisplay.Text = "3"
     Else
      txtDisplay.Text = txtDisplay.Text & "3"
      End If
End Sub

Private Sub cmd4_Click(Index As Integer)
If (txtDisplay.Text = 0) Then
    txtDisplay.Text = "4"
     Else
      txtDisplay.Text = txtDisplay.Text & "4"
      End If
End Sub

Private Sub cmd5_Click(Index As Integer)
If (txtDisplay.Text = 0) Then
    txtDisplay.Text = "5"
     Else
      txtDisplay.Text = txtDisplay.Text & "5"
      End If
End Sub

Private Sub cmd6_Click(Index As Integer)
If (txtDisplay.Text = 0) Then
    txtDisplay.Text = "6"
     Else
      txtDisplay.Text = txtDisplay.Text & "6"
      End If
End Sub

Private Sub cmd7_Click(Index As Integer)
If (txtDisplay.Text = 0) Then
    txtDisplay.Text = "7"
     Else
      txtDisplay.Text = txtDisplay.Text & "7"
      End If
End Sub

Private Sub cmd8_Click(Index As Integer)
If (txtDisplay.Text = 0) Then
    txtDisplay.Text = "8"
     Else
      txtDisplay.Text = txtDisplay.Text & "8"
      End If
End Sub

Private Sub cmd9_Click(Index As Integer)
If (txtDisplay.Text = 0) Then
    txtDisplay.Text = "9"
     Else
      txtDisplay.Text = txtDisplay.Text & "9"
      End If
End Sub



Private Sub cmdClear_Click()
UpToDateCalculation = 0
 txtDisplay.Text = 0
 Add = False
 TakeAway = False
 Times = False
 Divide = False
 Equals = False
 End Sub
Private Sub cmdCos_Click(Index As Integer)
txtDisplay.Text = Math.Cos(txtDisplay.Text)

End Sub

Private Sub cmdDivision_Click(Index As Integer)
If UpToDateCalculation = 0 Then
     UpToDateCalculation = Val(txtDisplay.Text)
     txtDisplay.Text = "0"
     Else
     If txtDisplay.Text = 0 Then
         Else
         UpToDateCalculation = UpToDateCalculation / Val(txtDisplay.Text)
         txtDisplay.Text = "0"
     End If
 End If
 Add = False
 TakeAway = False
 Times = False
 Divide = True

End Sub



Private Sub cmdDot_Click(Index As Integer)
If InStr(txtDisplay.Text, ".") = 0 Then
             txtDisplay.Text = txtDisplay.Text & "."
        End If

End Sub

Private Sub cmdEqual_Click(Index As Integer)
If Add = True Then
     If UpToDateCalculation = 0 Then
         UpToDateCalculation = Val(txtDisplay.Text)
         txtDisplay.Text = "0"
        Else
         UpToDateCalculation = UpToDateCalculation + Val(txtDisplay.Text)
     End If
     Else
     If TakeAway = True Then
        If UpToDateCalculation = 0 Then
            UpToDateCalculation = Val(txtDisplay.Text)
             Else
             UpToDateCalculation = UpToDateCalculation - Val(txtDisplay.Text)
         End If
        Else
         If Times = True Then
             If UpToDateCalculation = 0 Then
                 UpToDateCalculation = Val(txtDisplay.Text)
                 Else
                UpToDateCalculation = UpToDateCalculation * Val(txtDisplay.Text)
            End If
             Else
             If Divide = True Then
                 If UpToDateCalculation = 0 Then
                    UpToDateCalculation = Val(txtDisplay.Text)
                     Else
                    If txtDisplay.Text = 0 Then
                         Else
                        UpToDateCalculation = UpToDateCalculation / Val(txtDisplay.Text)
                     End If
                End If

             End If
         End If
     End If
 End If
 txtDisplay.Text = UpToDateCalculation
Add = False
 TakeAway = False
 Times = False
 Divide = False
 Equals = True
End Sub

Private Sub cmdMinus_Click(Index As Integer)
If UpToDateCalculation = 0 Then
     UpToDateCalculation = Val(txtDisplay.Text)
     txtDisplay.Text = "0"
     Else
     If txtDisplay.Text = 0 Then
         Else
         UpToDateCalculation = UpToDateCalculation - Val(txtDisplay.Text)
         txtDisplay.Text = "0"
     End If
 End If
 Add = False
 TakeAway = True
 Times = False
 Divide = False

End Sub

Private Sub cmdmultiplication_Click(Index As Integer)
 If UpToDateCalculation = 0 Then
     UpToDateCalculation = Val(txtDisplay.Text)
     txtDisplay.Text = "0"
     Else
     If txtDisplay.Text = 0 Then
         Else
         UpToDateCalculation = UpToDateCalculation * Val(txtDisplay.Text)
         txtDisplay.Text = "0"
     End If
 End If
 Add = False
 TakeAway = False
 Times = True
 Divide = False
End Sub

Private Sub cmdPercentage_Click(Index As Integer)


End Sub

Private Sub cmdPi_Click(Index As Integer)
If (txtDisplay.Text = 0) Then
    txtDisplay.Text = "3.1415926"
     Else
      txtDisplay.Text = txtDisplay.Text & "3.1415926"
  End If
 End Sub

Private Sub cmdPlus_Click(Index As Integer)
If UpToDateCalculation = 0 Then
      UpToDateCalculation = Val(txtDisplay.Text)
     txtDisplay.Text = "0"
      Else
      UpToDateCalculation = UpToDateCalculation + Val(txtDisplay.Text)
      txtDisplay.Text = "0"
  End If
  Add = True
  TakeAway = False
  Times = False
  Divide = False
 End Sub


Private Sub cmdSin_Click(Index As Integer)
Select Case Index
Dim x As Double
Case 0
x = Val(txtDisplay.Text)
txtDisplay.Text = Round((Sin(x * 3.14159265 / 180)), 4)

End Select

End Sub

Private Sub cmdSqrt_Click(Index As Integer)
txtDisplay.Text = Math.Sqr(txtDisplay.Text)

End Sub

Private Sub cmdTan_Click(Index As Integer)
txtDisplay.Text = Math.Tan(txtDisplay.Text)

End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: calculator in vb6 couple questions

#2 raziel_   User is offline

  • Like a lollipop
  • member icon

Reputation: 469
  • View blog
  • Posts: 4,281
  • Joined: 25-March 09

Re: calculator in vb6 couple questions

Posted 08 May 2012 - 07:04 AM

hi,
what do you mean by "I didn't know how to create percentage button"?
about how to clear the textbox by pressing any button i suggest you create a variable that will be of type bool. when you press any of the function keys ( +, -, / etc.) you set this var to true. and in your number keys check if this var is true if so clear the textbox(textbox.text="") and set the bool var to false again. this way when you press any of the function keys you will clear the textbox every time you start typing new number.

Good Luck.

This post has been edited by raziel_: 08 May 2012 - 07:05 AM

Was This Post Helpful? 1
  • +
  • -

#3 Neku   User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 269
  • Joined: 21-May 09

Re: calculator in vb6 couple questions

Posted 08 May 2012 - 07:17 AM

i think it will be better to make a single button for clearing the textbox or any time he try to type it will delete the numbers.
as for the percentage button, you'll need a formula for it that will take two numbers you type and will use them to calculate the percentage...

however i have a question? why almost every button have a diffrent (yet generic [aka name given by VB itself]) name but they seems to be part of an control array?
it dosent even seems like you use their index number...
Was This Post Helpful? 1
  • +
  • -

#4 raziel_   User is offline

  • Like a lollipop
  • member icon

Reputation: 469
  • View blog
  • Posts: 4,281
  • Joined: 25-March 09

Re: calculator in vb6 couple questions

Posted 08 May 2012 - 07:30 AM

@ Neku I dont think you understand what i mean let me explain it trough code:
Dim bClear As Boolean
bClear=false
Private Sub cmdPlus_Click()
'Sum some numbers and show the result
bClear=true
End Sub
Private Sub cmd2_Click(Index As Integer)
If (txtDisplay.Text = 0) Then
    If bClear=True Then
        textbox.text=""
        bClear=False
    End If
    txtDisplay.Text = "2"
     Else
      txtDisplay.Text = txtDisplay.Text & "2"
      End If
End Sub



This way only if he press the plus button it will clear the textbox. You can even use this in the equal button so when you press it the next time you start typing new number to clear the textbox it self as it is on the windows calculator.

This post has been edited by raziel_: 08 May 2012 - 07:33 AM

Was This Post Helpful? 0
  • +
  • -

#5 BobRodes   User is offline

  • Product Manager
  • member icon

Reputation: 603
  • View blog
  • Posts: 3,085
  • Joined: 19-May 09

Re: calculator in vb6 couple questions

Posted 08 May 2012 - 01:31 PM

That doesn't sound like the behavior of any calculator I've ever heard of. Calculators have a Clear and Clear Entry key. Why would you create a calculator that nobody knows how to use?

As for percentage button, all you have to do is divide the value in the text box by 100.
Was This Post Helpful? 0
  • +
  • -

#6 raziel_   User is offline

  • Like a lollipop
  • member icon

Reputation: 469
  • View blog
  • Posts: 4,281
  • Joined: 25-March 09

Re: calculator in vb6 couple questions

Posted 08 May 2012 - 11:38 PM

when you press "Winkey + R" the run dialog opens. Now when you type "calc" in it and press "Enter" key the windows calculator opens. Now when you type for example "7" then "8" then "9" and after this you press the "+" key. when you start typing the new number lets say "1" you will see how the old number (789) disappear. i dont know about you but every single calculator do this that i have seen in my humble life(except windows 7 calc that makes 2 rows).
Was This Post Helpful? 0
  • +
  • -

#7 Neku   User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 269
  • Joined: 21-May 09

Re: calculator in vb6 couple questions

Posted 09 May 2012 - 09:16 AM

it dosent delete it.. the number 789 in your example is stored in a variable.
what he wanted is to clear it by clicking any key, that would include clearing all variables that hold data for calculation
Was This Post Helpful? 0
  • +
  • -

#8 BobRodes   User is offline

  • Product Manager
  • member icon

Reputation: 603
  • View blog
  • Posts: 3,085
  • Joined: 19-May 09

Re: calculator in vb6 couple questions

Posted 09 May 2012 - 10:29 AM

Oh, I see what the OP is saying now. The way that calculators work is after pressing the = key (as well as in more advanced calculators function keys like square root), the result displayed is terminal, meaning that the next number pressed will start a new calculation. So, store a boolean value called isTerminal or whatever, and when a number is pressed, check this variable. If true, set to false, clear the text box, enter the number. If false, tack the number on the end of the text box.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1