# .vb calculator code



## dragonempres (Jun 23, 2003)

heys, i have to make a calculator for a school project but it won't work right, can anyone tell me whats wrong with this code?? .....


Public Class CalculatorForm
Inherits System.Windows.Forms.Form

Private mintFirstNum As Integer
Private mintSecondNum As Integer
Private mblnAdd As Boolean
Private mblnSubtract As Boolean
Private mblnMultiply As Boolean
Private mblnDivide As Boolean
Private chaSign As Char

Windows FormDesigner generated code

Private Sub ExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButton.Click
Me.Close()
End Sub

Private Sub ClearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearButton.Click
'prepare scree for next calculation
Me.DisplayLabel.Text = ""
End Sub

Private Sub ZeroButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ZeroButton.Click
Me.DisplayLabel.Text = Me.DisplayLabel.Text & "0"
End Sub

Private Sub OneButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OneButton.Click
Me.DisplayLabel.Text = Me.DisplayLabel.Text & "1"
End Sub

Private Sub TwoButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TwoButton.Click
Me.DisplayLabel.Text = Me.DisplayLabel.Text & "2"
End Sub

Private Sub ThreeButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ThreeButton.Click
Me.DisplayLabel.Text = Me.DisplayLabel.Text & "3"
End Sub

Private Sub FourButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FourButton.Click
Me.DisplayLabel.Text = Me.DisplayLabel.Text & "4"
End Sub


Private Sub FiveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FiveButton.Click
Me.DisplayLabel.Text = Me.DisplayLabel.Text & "5"
End Sub

Private Sub SixButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SixButton.Click
Me.DisplayLabel.Text = Me.DisplayLabel.Text & "6"
End Sub

Private Sub SevenButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SevenButton.Click
Me.DisplayLabel.Text = Me.DisplayLabel.Text & "7"
End Sub

Private Sub EightButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EightButton.Click
Me.DisplayLabel.Text = Me.DisplayLabel.Text & "8"
End Sub

Private Sub NineButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NineButton.Click
Me.DisplayLabel.Text = Me.DisplayLabel.Text & "9"
End Sub


Private Sub AddNumButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddNumButton.Click
'save the firstnumber
Me.DisplayLabel.Text = mintFirstNum
'clear the scree so user can enter second number
Me.DisplayLabel.Text = ""
'save the sign
mblnAdd = True
mblnSubtract = False
mblnMultiply = False
mblnDivide = False
'save second number
Me.DisplayLabel.Text = mintSecondNum
End Sub

Private Sub SubtNumButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SubtNumButton.Click
'save the firstnumber
Me.DisplayLabel.Text = mintFirstNum
'clear the scree so user can enter second number
Me.DisplayLabel.Text = ""
'save the sign
mblnAdd = False
mblnSubtract = True
mblnMultiply = False
mblnDivide = False
'save second number
Me.DisplayLabel.Text = mintSecondNum
End Sub

Private Sub MultNumButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MultNumButton.Click
'save the firstnumber
Me.DisplayLabel.Text = mintFirstNum
'clear the scree so user can enter second number
Me.DisplayLabel.Text = ""
'save the sign
mblnAdd = False
mblnSubtract = False
mblnMultiply = True
mblnDivide = False
'save second number
Me.DisplayLabel.Text = mintSecondNum
End Sub

Private Sub DivNumButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DivNumButton.Click
'save the firstnumber
Me.DisplayLabel.Text = mintFirstNum
'clear the scree so user can enter second number
Me.DisplayLabel.Text = ""
'save the sign
mblnAdd = False
mblnSubtract = False
mblnMultiply = False
mblnDivide = True
'save second number
Me.DisplayLabel.Text = mintSecondNum
End Sub

Private Sub EqualsButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EqualsButton.Click
If mblnAdd = True Then Me.DisplayLabel.Text = mintFirstNum + mintSecondNum _
Else If mblnSubtract = True Then Me.DisplayLabel.Text = mintFirstNum - mintSecondNum _
Else If mblnMultiply = True Then Me.DisplayLabel.Text = mintFirstNum * mintSecondNum _
Else If mblnDivide = True Then Me.DisplayLabel.Text = mintFirstNum \ mintSecondNum
End Sub
End Class


----------



## dragonempres (Jun 23, 2003)

mkays, i changed it so that it saves the second number when you hit the calc button and that solved one of my problems, but it still wont calculate/display the answer....

Private Sub EqualsButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EqualsButton.Click
'save second number 
Me.DisplayLabel.Text = mintSecondNum
'calculate answer 
If mblnAdd = True Then Me.DisplayLabel.Text = mintFirstNum + mintSecondNum _
Else If mblnSubtract = True Then Me.DisplayLabel.Text = mintFirstNum - mintSecondNum _
Else If mblnMultiply = True Then Me.DisplayLabel.Text = mintFirstNum * mintSecondNum _
Else If mblnDivide = True Then Me.DisplayLabel.Text = mintFirstNum \ mintSecondNum
End Sub


----------



## dragonempres (Jun 23, 2003)

save second number 
Me.DisplayLabel.Text = mintSecondNum
'clear screen 
Me.DisplayLabel.Text = ""
'calculate answer 
If mblnAdd = True Then
Me.DisplayLabel.Text = mintFirstNum + mintSecondNum
End If
If mblnSubtract = True Then
Me.DisplayLabel.Text = mintFirstNum - mintSecondNum
End If
If mblnMultiply = True Then
Me.DisplayLabel.Text = mintFirstNum * mintSecondNum
End If
If mblnDivide = True Then
Me.DisplayLabel.Text = mintFirstNum \ mintSecondNum
End If


i changed my equal buttons code to that, but it still wont calculate the answer. Can annyone help me, or does anyonw know a website where i could get some sample code to look at? i think my problem is either with how i'm saving the numbers, or preforming the calculations...please help if you can!!! thanks


----------



## AbvAvgUser (Oct 3, 2002)

Rather than giving the entire code, just give us the error that you are getting. That will make bug-fixing faster.


----------



## lamstaldy (Dec 16, 2001)

Try changing the lines that assign values to mintFirstNum and mintSecondNum from:

Me.DisplayLabel.Text = mintFirstNum 
Me.DisplayLabel.Text = mintSecondNum

to:

mintFirstNum = Me.DisplayLabel.Text
mintSecondNum = Me.DisplayLabel.Text

mintFirstNum = Me.DisplayLabel.Text takes the number from DisplayLabel and stores it in mintFirstNum. 

Me.DisplayLabel.Text = mintFirstNum takes the value from mintFirstNum and loads it into Me.DisplayLabel.Text.


----------

