Description

This application evaluates payroll, using the total time worked in one or two weeks by an employee and his or her hourly salary. The calculation takes overtime into account.

Practical LearningPractical Learning: Creating the Application

  1. Start Microsoft Visual Studio
  2. On the Visual Studio 2022 dialog box, click Create a New Project
  3. In the Create a New Project dialog box, in the Languages combo box, select Visual Basic
  4. In the list project templates, click Windows Forms App
  5. Click Next
  6. In the Configure Your New Project dialog box, set the Project Name to PayrollEvaluation1
  7. Click Next
  8. In the Additional Information dialog box, in the Framework combo box, select the highest version (.NET 9.0 (Standard Term Support)).
    Click Create
  9. In the Solution Explorer, right-click Form1.cs and click Rename
  10. Type Exercise (to get Exercise.cs) and press Enter twice
  11. Design the form as follows:
     

    Payroll Evaluatiion

    Control Name Text Other Properties
    GroupBox Group Box   Employee Identification  
    Label Label   &Employee Name:  
    TextBox TextBox TxtEmployeeName   AutoCompleteCustomSource
    Andy Barang
    Thomas Stones
    Gertrude Monay
    Christophe Yuen
    Micheline Hammond
    Paul Bertrand Yamaguchi

    AutoCompleteSource: Suggest
    AutoCompleteMode: CustomSource
    Label Label   Hourly &Salary:  
    TextBox TextBox TxtHourlySalary    
    GroupBox GroupBox   Time Values  
    Label Label   Monday  
    Label Label   Tuesday  
    Label Label   Wednesday  
    Label Label   Thursday  
    Label Label   Friday  
    Label Label   Saturday  
    Label Label   Sunday  
    Label Label   Work Week:  
    TextBox TextBox TxtMonday 0.00 TextAlign: Right
    TextBox TextBox TxtTuesday 0.00 TextAlign: Right
    TextBox TextBox TxtWednesday 0.00 TextAlign: Right
    TextBox TextBox TxtThursday 0.00 TextAlign: Right
    TextBox TextBox TxtFriday 0.00 TextAlign: Right
    TextBox TextBox TxtSaturday 0.00 TextAlign: Right
    TextBox TextBox TxtSunday 0.00 TextAlign: Right
    GroupBox GroupBox   Calculation  
    Label Label   Time  
    Label Label   Pay Amt  
    Label Label btnCalculate Calculate
    Label Label   Regular  
    TextBox TextBox TxtRegularTime 0.00 TextAlign: Right
    Enabled: False
    TextBox TextBox TxtRegularPay 0.00 TextAlign: Right
    Enabled: False
    Label Label   Net Pay:  
    TextBox TextBox TxtNetPay 0.00 TextAlign: Right
    Enabled: False
    Label Label   Overtime  
    TextBox TextBox TxtOvertime 0.00 TextAlign: Right
    Enabled: False
    TextBox TextBox TxtOvertimeAmount 0.00 TextAlign: Right
    Enabled: False
    Label Label btnClose   AutoSize: False
  12. On the form, double-click the Calculate label and implement its event as follows:
    Public Class Exercise
        Private Sub BtnCalculateClick(sender As Object, e As EventArgs) Handles BtnCalculate.Click
            Dim Monday As Double
            Dim Tuesday As Double
            Dim Wednesday As Double
            Dim Thursday As Double
            Dim Friday As Double
            Dim Saturday As Double
            Dim Sunday As Double
            Dim TotalTime As Double
    
            Dim RegularTime As Double
            Dim OverTime As Double
            Dim RegularPay As Double
            Dim OvertimePay As Double
    
            Dim HourlySalary As Double
    
            REM Get the hourly salary. Use exception handling in case the user types a bad value.
    
            Try
                HourlySalary = CDbl(TxtHourlySalary.Text)
            Catch fe As FormatException
                MsgBox("The value you typed for the salary is invalid. " &
                       "Please try again.", "Payroll Evaluation",
                       MsgBoxStyle.OkOnly Or MsgBoxStyle.Information)
    
                TxtHourlySalary.Focus()
            End Try
    
            REM Get the value for each work dayworked.
            REM Use exception handling for each text box in case the user types a bad value.
            Try
                Monday = CDbl(TxtMonday.Text)
            Catch fe As FormatException
                MsgBox("You typed an invalid value for Monday of the first week. " &
                       "Please try again.", "Payroll Evaluation",
                       MsgBoxStyle.OkOnly Or MsgBoxStyle.Information)
                TxtMonday.Focus()
            End Try
    
            Try
                Tuesday = CDbl(TxtTuesday.Text)
            Catch fe As FormatException
    
                MsgBox("You typed an invalid value for Tuesday of the first week. " &
                       "Please try again.", "Payroll Evaluation",
                       MsgBoxStyle.OkOnly Or MsgBoxStyle.Information)
                TxtTuesday.Focus()
            End Try
    
            Try
                Wednesday = CDbl(TxtWednesday.Text)
            Catch fe As FormatException
    
                MsgBox("You typed an invalid value for Wednesday of the first week. " &
                       "Please try again.", "Payroll Evaluation",
                       MsgBoxStyle.OkOnly Or MsgBoxStyle.Information)
                TxtWednesday.Focus()
            End Try
    
            Try
                Thursday = CDbl(TxtThursday.Text)
            Catch fe As FormatException
                MsgBox("You typed an invalid value for Thursday of the first week. " &
                       "Please try again.", "Payroll Evaluation",
                       MsgBoxStyle.OkOnly Or MsgBoxStyle.Information)
                TxtThursday.Focus()
            End Try
    
            Try
                Friday = CDbl(TxtFriday.Text)
            Catch fe As FormatException
                MsgBox("You typed an invalid value for Firday of the first week. " &
                       "Please try again.", "Payroll Evaluation",
                       MsgBoxStyle.OkOnly Or MsgBoxStyle.Information)
                TxtFriday.Focus()
            End Try
    
            Try
                Saturday = CDbl(TxtSaturday.Text)
            Catch ex As Exception
                MsgBox("You typed an invalid value for Saturday of the first week. " &
                       "Please try again.", "Payroll Evaluation",
                       MsgBoxStyle.OkOnly Or MsgBoxStyle.Information)
                TxtSaturday.Focus()
            End Try
    
            Try
                Sunday = CDbl(TxtSunday.Text)
            Catch fe As FormatException
                MsgBox("You typed an invalid value for Sunday of the first week. " &
                       "Please try again.", "Payroll Evaluation",
                       MsgBoxStyle.OkOnly Or MsgBoxStyle.Information)
                TxtSunday.Focus()
            End Try
    
            REM Calculate the total number of hours for each week
            TotalTime = Monday + Tuesday + Wednesday +
                             Thursday + Friday + Saturday + Sunday
    
            REM The overtime Is paid time And half
            Dim OvertimeSalary As Double = HourlySalary * 1.5
    
            REM  If the employee worked below 40 hours, there Is no overtime
            If TotalTime < 40 Then
                RegularTime = TotalTime
                RegularPay = HourlySalary * RegularTime
                OverTime = 0.00
                OvertimePay = 0.00
                REM  If the employee worked over 40 hours, calculate the overtime
            ElseIf TotalTime >= 40 Then
                RegularTime = 40
                RegularPay = HourlySalary * 40
                OverTime = TotalTime - 40
                OvertimePay = OverTime * OvertimeSalary
            End If
    
            Dim NetPay As Double = RegularPay + OvertimePay
    
            TxtRegularTime.Text = FormatNumber(regularTime)
            TxtOverTime.Text = FormatNumber(overTime)
            TxtRegularPay.Text = FormatCurrency(RegularPay)
            TxtOvertimePay.Text = FormatCurrency(OvertimePay)
    
            TxtNetPay.Text = FormatCurrency(NetPay)
        End Sub
    End Class
  13. Double-click the Close label and implement its Click event as follows:
    Private Sub BtnCloseClick(sender As Object, e As EventArgs) Handles BtnClose.Click
        End
    End Sub
  14. Execute the application to see the result

    Payroll Evaluation

  15. Enter the following values in the text boxes:
    Employee Name: Gertrude Monay
    Hourly Salary: 28.46
    Work Week
        Monday:    8
        Tuesday:   7.5
        Wednesday: 6
        Thursday:  7.5
        Friday:    6.5
        Saturday:  0
        Sunday:    0

    Payroll Evaluation

  16. Click the Calculate button:

    Payroll Evaluation

  17. Replace the values as follows:
    Employee Name: Thomas Stones
    Hourly Salary: 31.68
    Work Week
        Monday:    8
        Tuesday:   10.5
        Wednesday: 9
        Thursday:  8
        Friday:    8
        Saturday:  0
        Sunday:    0
  18. Click the Calculate button:

    Payroll Evaluation

  19. Close the form and return to your programming environment

Home Copyright © 2010-2025, FunctionX Thursday 12 December 2024, 09:34 Home