Shane Partridge
Visual Basic 2013
The Code
Public Class Form83
'## Example 20.2 : Shopping Cart ##
'## In this example, we add a few Labels, two Buttons, ##
'## and six Check Boxes. We declare the price of each item using the Const Keyword. ##
'## If a Checkbox is being ticked, its state is True else its state is False. ##
'## To calculate the total amount of purchase, ##
'## we use the mathematical operator +=. For example, sum+=BN is actually sum=sum+BN. ##
'## Finally, we use the ToString method to display the amount in currency. ##
'## Dimension Variables using (Const) Declaration ##
'## Indicate Value is a Constant & Cannot be Changed ##
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Const large As Integer = 10.0
Const medium As Integer = 8
Const small As Integer = 5
Dim sum As Integer
Dim c As String = "n2"
If CheckBox1.Checked = True Then
sum += large
End If
If CheckBox2.Checked = True Then
sum += medium
End If
If CheckBox3.Checked = True Then
sum += small
End If
Label1.Text = sum.ToString("c")
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
CheckBox1.Checked = False
CheckBox2.Checked = False
CheckBox3.Checked = False
Label1.Text = ""
End Sub
End Class