I am working on a homework assignment for Visual Basic using VB 2008. The application is a cash register. My problem is with the enter button click event handler.
Here is the code for the enter button as I have it right now:
Private Sub enterButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles enterButton.Click
'declare variable
Dim subtotal As Decimal
'add current amount to subtotal then clear price text box
subtotal = Val(subtotalResultLabel.Text) + Val(priceTextBox.Text)
subtotalResultLabel.Text = subtotal
priceTextBox.Clear()
End Sub
The goal here is to add the value from the priceTextBox to the value from the subtotalResultLabel and display the new subtotal in the subtotalResultLabel then clear the priceTextBox. Should be simple.
I have the line "commented out" that should convert the variable "subtotal" toa string in the currency format and display in a resultlabel. My problem is that with the code as it is right now, the calculation is correct -price entered adds to the subtotal and displays in the result label - but the format is not in currency.
When I "comment out" this line:
subtotalResultLabel.Text = subtotal
and use the other line instead, the result is displayed in currency, but the calculation doesn't work. I must be missing something with regards to implicit conversions... I know there has to be a simple answer - but so far it has escaped me. Anyone able to clue me in, please?
Thank you in advance for any help you may offer.
The complete code as I have it now is below
Thank you in advance for any help you may offer.
The complete code as I have it now is below
Public
Class CashRegisterForm
Private Sub zeroButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles zeroButton.Click
priceTextBox.Text &=
"0" 'concatenate "0" to display
End Sub 'zero button click
Private Sub oneButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles oneButton.Click
priceTextBox.Text &=
"1" 'concatenate "1" to display
End Sub 'one button click
Private Sub twoButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles twoButton.Click
priceTextBox.Text &=
"2" 'concatenate "2" to display
End Sub 'two button click
Private Sub threeButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles threeButton.Click
priceTextBox.Text &=
"3" 'concatenate "3" to display
End Sub ' three button click
Private Sub fourButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles fourButton.Click
priceTextBox.Text &=
"4" 'concatenate "4" to display
End Sub ' four button click
Private Sub fiveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles fiveButton.Click
priceTextBox.Text &=
"5" 'concatenate "5" to display
End Sub ' five button click
Private Sub sixButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sixButton.Click
priceTextBox.Text &=
"6" 'concatenate "6" to display
End Sub ' six button click
Private Sub sevenButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sevenButton.Click
priceTextBox.Text &=
"7" 'concatenate "7" to display
End Sub 'seven button click
Private Sub eightButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles eightButton.Click
priceTextBox.Text &=
"8" 'concatenate "8" to display
End Sub ' eight button click
Private Sub nineButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nineButton.Click
priceTextBox.Text &=
"9" 'concatenate "9" to display
End Sub ' nine button click
Private Sub pointButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pointButton.Click
priceTextBox.Text &=
"." 'concatenate "." to display
End Sub 'point button click
Private Sub enterButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles enterButton.Click
'declare variable
Dim subtotal As Decimal
'add current amount to subtotal then clear price text box
subtotal = Val(subtotalResultLabel.Text) + Val(priceTextBox.Text)
subtotalResultLabel.Text = subtotal
'subtotalResultLabel.Text = String.Format("{0:C}", subtotal)
priceTextBox.Clear()
End Sub 'enter button click
Private Sub totalButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles totalButton.Click
'declare variables
Dim subtotal As Decimal
Dim tax As Decimal
Dim total As Decimal
subtotal = Val(subtotalResultLabel.Text)
Select Case subtotal
Case Is < 100
tax = 0.1 * subtotal
Case 100 To 500
tax = 0.075 * subtotal
Case Else
tax = 0.05 * subtotal
End Select
'display tax
taxResultLabel.Text =
String.Format("{0:C}", tax)
'display total
total = (subtotal + tax)
totalResultLabel.Text =
String.Format("{0:C}", total)
End Sub 'total button click
Private Sub clearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clearButton.Click
'clear user input, subtotal, tax and total
priceTextBox.Clear()
subtotalResultLabel.Text =
""
taxResultLabel.Text =
""
totalResultLabel.Text =
""
End Sub 'clear button click
Private Sub deleteButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles deleteButton.Click
'delete user input
priceTextBox.Clear()
End Sub 'delete button click
End
Class ' CashRegisterForm