S
Steven Smith
I was flicking through the windows accesories programs
for some inspiration for todays vb challenge when I came
accross the calculator program & thought that looks easy
I could do that. However it hasn't turned out to be as
easy as I first anticipated due to a calculators strange
logic
I done a bit of research on the web to see what I could
come up with and I found this following page which
outlines the ideas behind a calculator utility:
http://cyberlearn.fau.edu/cafolla/VBDotNet/calculator1.htm
from that page I've came up with the following code so
far.
performing the previous mathamatical operation when the
current operator is selected however has me got beat, the
rest of the app works as this page suggests, I'm sure its
quite simple but I'm just not getting my head around it
at the minute as this is my first solo project working
outside course books...
anyway here's the code:
\\\
'declare form level variables
Dim mblnFirstDigit As Boolean = True
Dim mstrDisplay As String
Dim mstrLastOperator As String = "+"
Dim mintRunningTotal, mintCurrentAmount, mintDigit As
Integer
Private Sub FileExitMenuItem_Click(ByVal sender As
Object, ByVal e As System.EventArgs) _
Handles FileExitMenuItem.Click
Me.Close()
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal
e As System.EventArgs) _
Handles MyBase.Load
Me.DisplayLabel.Text = 0
End Sub
Private Sub DigitButtons_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
ThreeButton.Click, EightButton.Click, TwoButton.Click,
NineButton.Click, FourButton.Click, SixButton.Click,
FiveButton.Click, OneButton.Click, SevenButton.Click
'determine which numeric button has been pressed
If sender Is OneButton Then
mintDigit = 1
Call IsThisFirstDigit(sender, mintDigit)
ElseIf sender Is TwoButton Then
mintDigit = 2
Call IsThisFirstDigit(sender, mintDigit)
ElseIf sender Is ThreeButton Then
mintDigit = 3
Call IsThisFirstDigit(sender, mintDigit)
ElseIf sender Is FourButton Then
mintDigit = 4
Call IsThisFirstDigit(sender, mintDigit)
ElseIf sender Is FiveButton Then
mintDigit = 5
Call IsThisFirstDigit(sender, mintDigit)
ElseIf sender Is SixButton Then
mintDigit = 6
Call IsThisFirstDigit(sender, mintDigit)
ElseIf sender Is SevenButton Then
mintDigit = 7
Call IsThisFirstDigit(sender, mintDigit)
ElseIf sender Is EightButton Then
mintDigit = 8
Call IsThisFirstDigit(sender, mintDigit)
ElseIf sender Is NineButton Then
mintDigit = 9
Call IsThisFirstDigit(sender, mintDigit)
Else 'sender is zero button
mintDigit = 0
Call IsThisFirstDigit(sender, mintDigit)
End If
End Sub
Private Sub IsThisFirstDigit(ByVal sender, ByVal
mintDigit)
'determine whether or not button clicked is the
first digit
If mblnFirstDigit = False Then
'add number to the display
Me.DisplayLabel.Text = Me.DisplayLabel.Text &
mintDigit
Else
'make display equal the number
Me.DisplayLabel.Text = mintDigit
End If
'now its not the first digit
mblnFirstDigit = False
End Sub
Private Function Last_Operator(ByVal sender, ByRef
mstrLastOperator)
'determine the last operator to be clicked in
order to perform correct sum
Select Case True
Case sender Is Me.DivideButton
mstrLastOperator = "/"
Case sender Is Me.MultiplyButton
mstrLastOperator = "*"
Case sender Is Me.SubtractButton
mstrLastOperator = "-"
Case sender Is Me.AddButton
mstrLastOperator = "+"
End Select
Return (mstrLastOperator)
End Function
Private Sub OperatorButtons_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) _
Handles DivideButton.Click, SubtractButton.Click,
AddButton.Click, MultiplyButton.Click
'store the current value on the display to a
variable
mintCurrentAmount = Val(Me.DisplayLabel.Text)
'make the next digit entered the first in the
string
mblnFirstDigit = True
'call Private Function to determine last operator
here
'determine which operator has been clicked
Select Case True
Case sender Is Me.DivideButton
mintRunningTotal = mintRunningTotal /
mintCurrentAmount
Case sender Is Me.MultiplyButton
mintRunningTotal = mintRunningTotal *
mintCurrentAmount
Case sender Is Me.SubtractButton
mintRunningTotal = mintRunningTotal -
mintCurrentAmount
Case Else 'sender is me.addbutton
mintRunningTotal = mintRunningTotal +
mintCurrentAmount
End Select
End Sub
Private Sub CancelButton_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) _
Handles CancelButton.Click
Me.DisplayLabel.Text = 0
mblnFirstDigit = True
End Sub
Private Sub EqualsButton_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) _
Handles EqualsButton.Click
'display the running total
Me.DisplayLabel.Text = mintRunningTotal
End Sub
End Class
any suggestions or guidance is as always appreciated
Regards Steve...
for some inspiration for todays vb challenge when I came
accross the calculator program & thought that looks easy
I could do that. However it hasn't turned out to be as
easy as I first anticipated due to a calculators strange
logic
I done a bit of research on the web to see what I could
come up with and I found this following page which
outlines the ideas behind a calculator utility:
http://cyberlearn.fau.edu/cafolla/VBDotNet/calculator1.htm
from that page I've came up with the following code so
far.
performing the previous mathamatical operation when the
current operator is selected however has me got beat, the
rest of the app works as this page suggests, I'm sure its
quite simple but I'm just not getting my head around it
at the minute as this is my first solo project working
outside course books...
anyway here's the code:
\\\
'declare form level variables
Dim mblnFirstDigit As Boolean = True
Dim mstrDisplay As String
Dim mstrLastOperator As String = "+"
Dim mintRunningTotal, mintCurrentAmount, mintDigit As
Integer
Private Sub FileExitMenuItem_Click(ByVal sender As
Object, ByVal e As System.EventArgs) _
Handles FileExitMenuItem.Click
Me.Close()
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal
e As System.EventArgs) _
Handles MyBase.Load
Me.DisplayLabel.Text = 0
End Sub
Private Sub DigitButtons_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
ThreeButton.Click, EightButton.Click, TwoButton.Click,
NineButton.Click, FourButton.Click, SixButton.Click,
FiveButton.Click, OneButton.Click, SevenButton.Click
'determine which numeric button has been pressed
If sender Is OneButton Then
mintDigit = 1
Call IsThisFirstDigit(sender, mintDigit)
ElseIf sender Is TwoButton Then
mintDigit = 2
Call IsThisFirstDigit(sender, mintDigit)
ElseIf sender Is ThreeButton Then
mintDigit = 3
Call IsThisFirstDigit(sender, mintDigit)
ElseIf sender Is FourButton Then
mintDigit = 4
Call IsThisFirstDigit(sender, mintDigit)
ElseIf sender Is FiveButton Then
mintDigit = 5
Call IsThisFirstDigit(sender, mintDigit)
ElseIf sender Is SixButton Then
mintDigit = 6
Call IsThisFirstDigit(sender, mintDigit)
ElseIf sender Is SevenButton Then
mintDigit = 7
Call IsThisFirstDigit(sender, mintDigit)
ElseIf sender Is EightButton Then
mintDigit = 8
Call IsThisFirstDigit(sender, mintDigit)
ElseIf sender Is NineButton Then
mintDigit = 9
Call IsThisFirstDigit(sender, mintDigit)
Else 'sender is zero button
mintDigit = 0
Call IsThisFirstDigit(sender, mintDigit)
End If
End Sub
Private Sub IsThisFirstDigit(ByVal sender, ByVal
mintDigit)
'determine whether or not button clicked is the
first digit
If mblnFirstDigit = False Then
'add number to the display
Me.DisplayLabel.Text = Me.DisplayLabel.Text &
mintDigit
Else
'make display equal the number
Me.DisplayLabel.Text = mintDigit
End If
'now its not the first digit
mblnFirstDigit = False
End Sub
Private Function Last_Operator(ByVal sender, ByRef
mstrLastOperator)
'determine the last operator to be clicked in
order to perform correct sum
Select Case True
Case sender Is Me.DivideButton
mstrLastOperator = "/"
Case sender Is Me.MultiplyButton
mstrLastOperator = "*"
Case sender Is Me.SubtractButton
mstrLastOperator = "-"
Case sender Is Me.AddButton
mstrLastOperator = "+"
End Select
Return (mstrLastOperator)
End Function
Private Sub OperatorButtons_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) _
Handles DivideButton.Click, SubtractButton.Click,
AddButton.Click, MultiplyButton.Click
'store the current value on the display to a
variable
mintCurrentAmount = Val(Me.DisplayLabel.Text)
'make the next digit entered the first in the
string
mblnFirstDigit = True
'call Private Function to determine last operator
here
'determine which operator has been clicked
Select Case True
Case sender Is Me.DivideButton
mintRunningTotal = mintRunningTotal /
mintCurrentAmount
Case sender Is Me.MultiplyButton
mintRunningTotal = mintRunningTotal *
mintCurrentAmount
Case sender Is Me.SubtractButton
mintRunningTotal = mintRunningTotal -
mintCurrentAmount
Case Else 'sender is me.addbutton
mintRunningTotal = mintRunningTotal +
mintCurrentAmount
End Select
End Sub
Private Sub CancelButton_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) _
Handles CancelButton.Click
Me.DisplayLabel.Text = 0
mblnFirstDigit = True
End Sub
Private Sub EqualsButton_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) _
Handles EqualsButton.Click
'display the running total
Me.DisplayLabel.Text = mintRunningTotal
End Sub
End Class
any suggestions or guidance is as always appreciated
Regards Steve...