Tuesday, March 10, 2026

Student Tuition Fee Calculator in Visual Basic NET

 








Program Listing

Public Class Form1

    ' Define Constants based on the scenario
    Private Const COST_PER_UNIT As Decimal = 1250D
    Private Const MISC_FEE As Decimal = 3500D
    Private Const LAB_FEE As Decimal = 2000D
    Private Const SCHOLAR_DISCOUNT_RATE As Decimal = 0.05D ' 5%

    Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
        Try
            ' 1. Get and Validate Input
            Dim unitsEnrolled As Integer
            If Not Integer.TryParse(txtUnits.Text, unitsEnrolled) Then
                MessageBox.Show("Please enter a valid number of units.")
                Exit Sub
            End If

            ' Scenario check: 15 units Min, 30 units Max (implied by "30 units Min" typo in image)
            If unitsEnrolled < 15 Or unitsEnrolled > 30 Then
                MessageBox.Show("Units must be between 15 and 30.")
                Exit Sub
            End If

            ' 2. Calculations
            ' Multiply number of units by cost per unit
            Dim tuitionBase = unitsEnrolled * COST_PER_UNIT

            ' Add miscellaneous and laboratory fees
            Dim totalBeforeDiscount = tuitionBase + MISC_FEE + LAB_FEE

            ' Compute discount if applicable
            Dim discountAmount = 0D
            If chkScholar.Checked Then
                discountAmount = totalBeforeDiscount * SCHOLAR_DISCOUNT_RATE
            End If

            Dim finalAmount = totalBeforeDiscount - discountAmount

            ' 3. Display Results
            lblResult.Text = $"Tuition ({unitsEnrolled} units): ₱{tuitionBase:N2}" & vbCrLf &
                             $"Fees (Misc + Lab): ₱{MISC_FEE + LAB_FEE:N2}" & vbCrLf &
                             $"Discount (5%): -₱{discountAmount:N2}" & vbCrLf &
                             $"----------------------------" & vbCrLf &
                             $"TOTAL TO PAY: ₱{finalAmount:N2}"

        Catch ex As Exception
            MessageBox.Show("An error occurred: " & ex.Message)
        End Try

    End Sub

    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        txtUnits.Text = ""
        lblResult.Text = ""
        chkScholar.Checked = False
        txtUnits.Focus()

    End Sub

    Private Sub btnQuity_Click(sender As Object, e As EventArgs) Handles btnQuity.Click
        If MessageBox.Show("Are you sure you want to exit?", "Exit", MessageBoxButtons.YesNo) = DialogResult.Yes Then
            Me.Close()
        End If
        txtUnits.Text = ""
        lblResult.Text = ""
        chkScholar.Checked = False
        txtUnits.Focus()
    End Sub

    Private Sub btnVision_Mission_Click(sender As Object, e As EventArgs) Handles btnVision_Mission.Click
        Form2.Show()
    End Sub
End Class