VERSION 5.00
Begin VB.Form frmCinema 
   Caption         =   "Cinema Ticket Sales"
   ClientHeight    =   3600
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   7335
   LinkTopic       =   "Form1"
   LockControls    =   -1  'True
   ScaleHeight     =   3600
   ScaleWidth      =   7335
   StartUpPosition =   3  'Windows Default
   Begin VB.CommandButton cmdSessionTotal 
      Caption         =   "Session Total"
      Height          =   375
      Left            =   4440
      TabIndex        =   14
      Top             =   2880
      Width           =   1335
   End
   Begin VB.CommandButton cmdChangeDue 
      Caption         =   "Change Due"
      Height          =   375
      Left            =   2700
      TabIndex        =   13
      Top             =   2880
      Width           =   1335
   End
   Begin VB.CommandButton cmdTicketCost 
      Caption         =   "Ticket Cost"
      Height          =   375
      Left            =   900
      TabIndex        =   12
      Top             =   2880
      Width           =   1335
   End
   Begin VB.TextBox txtTotalCollected 
      Height          =   285
      Left            =   5370
      TabIndex        =   11
      Top             =   1920
      Width           =   1815
   End
   Begin VB.TextBox txtChange 
      Height          =   285
      Left            =   2010
      TabIndex        =   10
      Top             =   1920
      Width           =   1725
   End
   Begin VB.TextBox txtAmtTendered 
      Height          =   285
      Left            =   2010
      TabIndex        =   9
      Top             =   1590
      Width           =   1725
   End
   Begin VB.TextBox txtTicketsCost 
      Height          =   285
      Left            =   2010
      TabIndex        =   8
      Top             =   900
      Width           =   1725
   End
   Begin VB.TextBox txtNoChildren 
      Height          =   285
      Left            =   2010
      TabIndex        =   7
      Top             =   450
      Width           =   1725
   End
   Begin VB.TextBox txtNoAdults 
      Height          =   285
      Left            =   2010
      TabIndex        =   6
      Top             =   120
      Width           =   1725
   End
   Begin VB.Label lblTotalCollected 
      Caption         =   "Total Now Collected"
      Height          =   225
      Left            =   5400
      TabIndex        =   5
      Top             =   1620
      Width           =   1785
   End
   Begin VB.Label lblChange 
      Caption         =   "Change Given"
      Height          =   225
      Left            =   180
      TabIndex        =   4
      Top             =   1920
      Width           =   1785
   End
   Begin VB.Label lblAmtTendered 
      Caption         =   "Amount Tendered"
      Height          =   225
      Left            =   180
      TabIndex        =   3
      Top             =   1620
      Width           =   1785
   End
   Begin VB.Label lblTicketsCost 
      Caption         =   "Cost of Tickets"
      Height          =   225
      Left            =   180
      TabIndex        =   2
      Top             =   930
      Width           =   1785
   End
   Begin VB.Label lblNoChildren 
      Caption         =   "Number of Children/Concession"
      Height          =   405
      Left            =   180
      TabIndex        =   1
      Top             =   480
      Width           =   1785
   End
   Begin VB.Label lblNoAdults 
      Caption         =   "Number of Adults"
      Height          =   225
      Left            =   180
      TabIndex        =   0
      Top             =   150
      Width           =   1785
   End
End
Attribute VB_Name = "frmCinema"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Private Const COST_PER_ADULT As Double = 14
Private Const COST_PER_CHILD As Double = 7

Private Const TOTAL_COLLECTED_SHOW As String = "Session Total"
Private Const TOTAL_COLLECTED_HIDE As String = "Hide Total"

Private Const KEY_BACKSPACE As Integer = 8
Private Const KEY_DECIMALPOINT As Integer = 46

Private m_dblSessionTotal As Double

Private Sub Form_Load()
    ' Initialise the form.
    init
End Sub

Private Sub cmdChangeDue_Click()
    ' Calculate the change.
    If (txtAmtTendered.Text <> vbNullString) And (txtTicketsCost.Text <> vbNullString) Then
        ' We can't process if the the tendered amount is less than required.
        If Val(txtAmtTendered.Text) < Val(txtTicketsCost.Text) Then
            MsgBox "The amount tendered is less than the ticket's cost.", vbExclamation + vbOKOnly, "Invalid Input"
        Else
            txtChange.Text = Format$(calcChange(txtAmtTendered.Text, txtTicketsCost.Text), "#,##0.00")
        End If
    Else
        MsgBox "Please calculate the ticket cost and enter the amount tendered.", vbExclamation + vbOKOnly, "Missing Input"
    End If
End Sub

Private Sub cmdSessionTotal_Click()
    ' Hides/shows the session total.
    
    If (cmdSessionTotal.Caption = TOTAL_COLLECTED_SHOW) = True Then
        updateSessionTotal m_dblSessionTotal
        setTotalCollectedVisible True
        
        cmdSessionTotal.Caption = TOTAL_COLLECTED_HIDE
    Else
        setTotalCollectedVisible False
        
        cmdSessionTotal.Caption = TOTAL_COLLECTED_SHOW
    End If
End Sub

Private Sub cmdTicketCost_Click()
    ' Calculate the cost of the ticket.
    txtTicketsCost.Text = Format$(calcTicketCost(txtNoAdults.Text, txtNoChildren.Text), "#,##0.00")
End Sub

Private Sub txtAmtTendered_KeyPress(KeyAscii As Integer)
    ' Validate the key.
    KeyAscii = validateKeyPressed(txtAmtTendered.Text, KeyAscii, True)
End Sub

Private Sub txtNoAdults_GotFocus()
    ' Clear the controls for a new entry.
    clearControls
End Sub

Private Sub txtNoAdults_KeyPress(KeyAscii As Integer)
    ' Validate the key.
    KeyAscii = validateKeyPressed(txtNoAdults.Text, KeyAscii, False)
End Sub

Private Sub txtNoAdults_Validate(Cancel As Boolean)
    ' Ensure that the user has entered something.
    If txtNoAdults.Text = vbNullString Then
        MsgBox "Please enter the number of adults.", vbExclamation + vbOKOnly, "Missing Input"
        
        ' And keep focus in the text box.
        Cancel = True
    End If
End Sub

Private Sub txtNoChildren_KeyPress(KeyAscii As Integer)
    ' Validate the key.
    KeyAscii = validateKeyPressed(txtNoChildren.Text, KeyAscii, False)
End Sub

Private Sub txtNoChildren_Validate(Cancel As Boolean)
    ' Ensure that the user has entered something.
    If txtNoChildren.Text = vbNullString Then
        MsgBox "Please enter the number of children.", vbExclamation + vbOKOnly, "Missing Input"
        
        ' And keep focus in the text box.
        Cancel = True
    End If
End Sub

Private Function calcChange(ByVal dblTendered As Double, _
                                               ByVal dblTotal As Double) As Double
    ' Calculates the change.
    Dim dblChange As Double
    
    dblChange = dblTendered - dblTotal
    m_dblSessionTotal = m_dblSessionTotal + dblTotal
    
    updateSessionTotal m_dblSessionTotal
    
    calcChange = dblChange
End Function

Private Function calcTicketCost(ByVal intNoAdults As Integer, _
                                                   ByVal intNoChildren As Integer) As Double
    ' Calculates the total ticket cost.
    Dim dblTotal As Double
    
    dblTotal = (intNoAdults * COST_PER_ADULT) + (intNoChildren * COST_PER_CHILD)
    
    calcTicketCost = dblTotal
End Function

Private Sub clearControls()
    ' Clears the controls for a new entry.
    Dim objControl As Control
    
    For Each objControl In Controls
        If TypeOf objControl Is TextBox Then
            If objControl.Name <> "txtTotalCollected" Then
                objControl.Text = vbNullString
            End If
        End If
    Next
End Sub

Private Sub init()
    ' Initialises the form & controls.
    setTotalCollectedVisible False
    
    ' The user shouldn't be able to enter data into
    ' the cost of tickets or the change given - so we're going to lock them.
    txtTicketsCost.Locked = True
    txtTicketsCost.ForeColor = vbGrayText
    
    txtChange.Locked = True
    txtChange.ForeColor = vbGrayText
End Sub

Private Sub setTotalCollectedVisible(ByVal blnVisible As Boolean)
    ' Hides/shows the total collected controls according to the parameter.
    lblTotalCollected.Visible = blnVisible
    txtTotalCollected.Visible = blnVisible
End Sub

Private Sub updateSessionTotal(ByVal dblTotal As Double)
    ' Updates the text box that displays the session total.
    txtTotalCollected.Text = Format$(dblTotal, "#,##0.00")
End Sub

Private Function validateKeyPressed(ByVal strText As String, _
                                                            ByVal intKeyAscii As Integer, _
                                                            ByVal blnAllowDecimals As Boolean) As Integer
    ' Validates the key that user has hit. The key is ignored if:
    ' -- If it's not a numeric key.
    ' -- If blnAllowDecimals is False & the user hits the "." key.
    ' Ignoring the key press is done by setting KeyAscii (in the KeyPress event)
    ' to 0 (zero). This function will return 0 if the validation fails.
    
    ' Since the backspace key is not numeric, we must explicitly allow it.
    If intKeyAscii = KEY_BACKSPACE Then
        validateKeyPressed = intKeyAscii
    Else
        ' Check to see if the value is numeric.
        If IsNumeric(Chr$(intKeyAscii)) = False Then
            If intKeyAscii = KEY_DECIMALPOINT Then
                If blnAllowDecimals = False Then
                    validateKeyPressed = 0
                Else
                    ' Only allow 1 decimal point.
                    If InStr(1, strText, ".") > 0 Then
                        ' Already as decimal point in the text.
                        validateKeyPressed = 0
                    Else
                        validateKeyPressed = intKeyAscii
                    End If
                End If
            Else
                validateKeyPressed = 0
            End If
        Else
            ' User pressed a numeric key.
            validateKeyPressed = intKeyAscii
        End If
    End If
End Function
