Shane Partridge
Visual Basic 2013
The Code
Public Class Form83
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'## Example 20.1 : 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 ##
Const LX As Integer = 100
Const BN As Integer = 500
Const SD As Integer = 200
Const HD As Integer = 80
Const HM As Integer = 300
Const AM As Integer = 150
Dim c As String = "n2"
Dim sum As Integer
'##Conditional Operator using If... Then... Else Operator. ##
'## (sum += value) implies as (Sum = Sum + Value) ##
If CheckBox1.Checked = True Then
sum += LX
End If
If CheckBox2.Checked = True Then
sum += BN
End If
If CheckBox3.Checked = True Then
sum += SD
End If
If CheckBox4.Checked = True Then
sum += HD
End If
If CheckBox5.Checked = True Then
sum += HM
End If
If CheckBox6.Checked = True Then
sum += AM
End If
Label1.Text = sum.ToString("c")
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'## Button 2 - Clear CheckBox ##
CheckBox1.Checked = False
CheckBox2.Checked = False
CheckBox3.Checked = False
CheckBox4.Checked = False
CheckBox5.Checked = False
CheckBox6.Checked = False
Label1.Text = ""
End Sub
End Class