Introduction

We are learning to program in Visual Basic. We are going to create a form that contains Windows controls for a time sheet that spans two weeks. From that form, we will get the time worked for each day. We will then perform different calculations for a payroll and present a pay summary.

Practical LearningPractical Learning: Creating the Application

  1. Start Microsoft Visual Studio
  2. On the Visual Studio 2026 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:

    Visual Studio 2026 - Create a New Project

  5. Click Next
  6. In the Configure Your New Project dialog box, set the Project Name to PayrollEvaluation2

    Visual Studio 2026 - Create a New Project

  7. Click Next
  8. In the Additional Information dialog box, in the Framework combo box, select the highest version (.NET 10.0 (Long 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. Right-click the body of the form and click Properties:

    Form - Properties

  12. In the Proeprties window, set the Text value to Payroll Evaluation

    Visual Studio 2026 - Create a New Project

  13. In the StartPosition combo box, select CenterScreen

    Form - Properties

  14. Still in the Properties window for the form, double-click MaximizeBox to set it to True

    Form - Properties

  15. In the Toolbox, from the Containers section, click the GroupBox Group Box and click the form
  16. In the Properties window, change the Text characteristic of the group box to Employee Identification
  17. Position the Employee Identification group box as follows:

    Payroll Evaluatiion

  18. From the Toolbox, click the Label Label and click inside the group box
  19. In the Properties window, change the Text field to &Employee Name:
  20. Position the Employee Name label in the Employee Identification group box as follows:

    Payroll Evaluatiion

  21. From the Toolbox, click the TextBox TextBox and click inside the group box
  22. In the Properties window, change the (Name) to TxtEmployeeName
  23. Position the Employee Name text box in the Employee Identification group box as follows:

    Payroll Evaluatiion

  24. From the Toolbox, click the Label Label and click inside the group box
  25. In the Properties window, change the Text field to Hourly &Salary:
  26. Position the Hourly Salary label in the Employee Identification group box as follows:

    Payroll Evaluatiion

  27. From the Toolbox, click the TextBox TextBox and click inside the group box
  28. In the Properties window, click (Name) and type TxtHourlySalary
  29. Still in the Properties window, change the Text field to 0.00
  30. Still in the Properties window for the Hourly Salary text box, double-click TextAlign to set it to Right

    Form - Properties

  31. Position the Hourly Salary text box in the Employee Identification group box as follows:

    Payroll Evaluatiion

  32. In the Toolbox, click the GroupBox Group Box and click the form
  33. In the Properties window, change the Text characteristic of the group box to Time Worked
  34. Position the Time Values group box as follows:

    Payroll Evaluatiion

  35. In the Toolbox, click the Label Label control
  36. Press and hold Ctrl
  37. Click inside the Time Values group box nine times
  38. In the Toolbox, click the Pointer Pointer
  39. On the form, click each label and, in the Properties window, change their Text values as follows:
    Monday
    Tuesday
    Wednesday
    Thursday
    Friday
    Saturday
    Sunday
    First Week:
    Second Week:
  40. Position the labels in the Time Values group box as follows:

    Payroll Evaluatiion

  41. In the Toolbox, click the TextBox control TextBox
  42. Press and hold Ctrl
  43. Click inside the Time Values group box fourteen times
  44. In the Toolbox, click the Pointer Pointer
  45. In the Time Values group box in the form, click each text box and, in the Properties window, change its characteristics as follows:

    Text: 0.00
    TextAlign: Right
  46. Position the text boxes in the Time Values group box and specify their names as follows:

    Payroll Evaluatiion

    TxtWeek1Monday
    TxtWeek1Tuesday
    TxtWeek1Wednesday
    TxtWeek1Thursday
    TxtWeek1Friday
    TxtWeek1Saturday
    TxtWeek1Sunday
    TxtWeek2Monday
    TxtWeek2Tuesday
    TxtWeek2Wednesday
    TxtWeek2Thursday
    TxtWeek2Friday
    TxtWeek2Saturday
    TxtWeek2Sunday
  47. In the Toolbox, from the Containers section, click the GroupBox Group Box and click the form
  48. From the Toolbox, click the Button control Label and click the form
  49. In the Properties window, change the following values and position the button as follows:

    FlatStyle System
    Text: Calculate
    (Name) BtnCalculate

  50. In the Properties window, change the Text characteristic of the group box to Payroll Results
  51. Position the Employee Identification group box as follows:

    Payroll Evaluatiion

  52. From the Toolbox, click the Label control Label and click inside the bottom group box
  53. In the Properties window, change the Text value to Time and position the label as follows:

    Payroll Evaluation - 2 Weeks

  54. From the Toolbox, click the Label control Label and click inside the bottom group box
  55. In the Properties window, change the Text value to Pay Amt and position the label as follows:

    Payroll Evaluation - 2 Weeks

  56. From the Toolbox, click the Label control Label and click inside the bottom group box
  57. In the Properties window, change the Text value to First Week and position the label as follows:

    Payroll Evaluation - 2 Weeks

  58. Add another Label control Label in the Payroll Results group box
  59. Set its Text property to Regular: and position the text box as follows:

    Payroll Evaluation - 2 Weeks

  60. Add a TextBox control TextBox in the Payroll Results group box
  61. In the Properties window, change the following characteristics and position the text box as follows::

    TextAlign: Right
    (Name): TxtWeek1RegularTime
    Enabled: False

    Payroll Evaluation

  62. In the Payroll Results group box, right-click the text box you just added and click Copy
  63. Right-click inside the Payroll Results group box and click Paste
  64. In the Properties window, change the (Name) to TxtWeek1RegularPay and position the text box as follows:

    Payroll Evaluation

  65. On the form, select the Regular label and the two text boxes on its right:

    Payroll Evaluation - 2 Weeks

  66. Copy the selection and paste it in the Payroll Results group box
  67. In the Properties window, change each (Name) property and position the controls as follows:

    Label Text: Overtime
    Text Box (Name): TxtWeek1OverTime
    Text Box (Name): TxtWeek1OvertimePay

    Payroll Evaluation

  68. On the form, select the Overtime label and the two text box on its right:

    Payroll Evaluation - 2 Weeks

  69. Copy the selection and paste it in the Payroll Results group box
  70. In the Properties window, change its properties and position the controls as follows:

    Label Text: Overtime
    Text Box (Name): TxtWeek1OverTime
    Text Box (Name): TxtWeek1OvertimePay

    Payroll Evaluation

  71. Add another Label control Label to the Payroll Results group box
  72. Set its Text to ________________________ to make it a horizontal line

    Payroll Evaluation - 2 Weeks

  73. On the form, right-click the First Week label in the Payroll Results group box and click Copy
  74. Press and hold Shift
  75. Click the other labels and text boxes except Time and Pay Amt
  76. Release Shift

    Payroll Evaluation

  77. Right-click the selection and click Copy
  78. Right-click inside the Payroll Results group box and click Paste

    Payroll Evaluation

  79. In the Properties window, change the Text value of the First Week label to Second Week:
  80. Change the names of the text boxes as follows:
    TxtWeek2RegularTime
    TxtWeek2RegularPay
    TxtWeek2OverTime
    TxtWeek2OvertimePay
    TxtWeek2Pay

    Payroll Evaluation

  81. From the Toolbox, add a Button to the Payroll Results group box of the form
  82. In the Properties window, change the following values:

    FlatStyle System
    Text: Close
    (Name) BtnClose

  83. On the form, select one of the Weekly Pay labels and the text box on its right:

    Payroll Evaluation - 2 Weeks

  84. Copy the selection and paste it in the Payroll Results group box
  85. In the Properties window, change the properties and position the controls as follows:

    Control (Name)
    Label Net Pay
    Text Box TxtNetPay

    Payroll Evaluation

  86. On the form, double-click the Calculate button
  87. Implement its event as follows:
    Public Class Exercise
        Private Sub BtnCalculate_Click(sender As Object, e As EventArgs) Handles BtnCalculate.Click
            Dim Week1Monday As Double
            Dim Week1Tuesday As Double
            Dim Week1Wednesday As Double
            Dim Week1Thursday As Double
            Dim Week1Friday As Double
            Dim Week1Saturday As Double
            Dim Week1Sunday As Double
            Dim Week2Monday As Double
            Dim Week2Ttuesday As Double
            Dim Week2Wednesday As Double
            Dim Week2Thursday As Double
            Dim Week2Friday As Double
            Dim Week2Saturday As Double
            Dim Week2Sunday 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
                Week1Monday = CDbl(TxtWeek1Monday.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)
                TxtWeek1Monday.Focus()
            End Try
    
            Try
                Week1Tuesday = CDbl(TxtWeek1Tuesday.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)
                TxtWeek1Tuesday.Focus()
            End Try
    
            Try
                Week1Wednesday = CDbl(TxtWeek1Wednesday.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)
                TxtWeek1Wednesday.Focus()
            End Try
    
            Try
                Week1Thursday = CDbl(TxtWeek1Thursday.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)
                TxtWeek1Thursday.Focus()
            End Try
    
            Try
                Week1Friday = CDbl(TxtWeek1Friday.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)
                TxtWeek1Friday.Focus()
            End Try
    
            Try
                Week1Saturday = CDbl(TxtWeek1Saturday.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)
                TxtWeek1Saturday.Focus()
            End Try
    
            Try
                Week1Sunday = CDbl(TxtWeek1Sunday.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)
                TxtWeek1Sunday.Focus()
            End Try
    
            Try
                Week2Monday = CDbl(TxtWeek2Monday.Text)
            Catch fe As FormatException
                MsgBox("You typed an invalid value for Monday of the second week. " &
                       "Please try again.", "Payroll Evaluation",
                       MsgBoxStyle.OkOnly Or MsgBoxStyle.Information)
                TxtWeek2Monday.Focus()
            End Try
    
            Try
                Week2Ttuesday = CDbl(TxtWeek2Tuesday.Text)
            Catch fe As FormatException
                MsgBox("You typed an invalid value for Tuesday of the second week. " &
                       "Please try again.", "Payroll Evaluation",
                       MsgBoxStyle.OkOnly Or MsgBoxStyle.Information)
                TxtWeek2Tuesday.Focus()
            End Try
    
            Try
                Week2Wednesday = CDbl(TxtWeek2Wednesday.Text)
            Catch fe As FormatException
                MsgBox("You typed an invalid value for Wednesday of the second week. " &
                       "Please try again.", "Payroll Evaluation",
                       MsgBoxStyle.OkOnly Or MsgBoxStyle.Information)
                TxtWeek2Wednesday.Focus()
            End Try
    
            Try
                Week2Thursday = CDbl(TxtWeek2Thursday.Text)
            Catch fe As FormatException
                MsgBox("You typed an invalid value for Thursday of the second week. " &
                       "Please try again.", "Payroll Evaluation",
                       MsgBoxStyle.OkOnly Or MsgBoxStyle.Information)
                TxtWeek2Thursday.Focus()
            End Try
    
            Try
                Week2Friday = CDbl(TxtWeek2Friday.Text)
            Catch fe As FormatException
                MsgBox("You typed an invalid value for Friday of the second week. " &
                       "Please try again.", "Payroll Evaluation",
                       MsgBoxStyle.OkOnly Or MsgBoxStyle.Information)
                TxtWeek2Friday.Focus()
            End Try
    
            Try
                Week2Saturday = CDbl(TxtWeek2Saturday.Text)
            Catch fe As FormatException
                MsgBox("You typed an invalid value for Saturday of the second week. " &
                       "Please try again.", "Payroll Evaluation",
                       MsgBoxStyle.OkOnly Or MsgBoxStyle.Information)
                TxtWeek2Saturday.Focus()
            End Try
    
            Try
                Week2Sunday = CDbl(TxtWeek2Sunday.Text)
            Catch fe As FormatException
                MsgBox("You typed an invalid value for Sunday of the second week. " &
                       "Please try again.", "Payroll Evaluation",
                       MsgBoxStyle.OkOnly Or MsgBoxStyle.Information)
                TxtWeek2Sunday.Focus()
            End Try
    
            Dim Week1RegularTime As Double
            Dim Week1OverTime As Double
            Dim Week1RegularPay As Double
            Dim Week1OvertimePay As Double
    
            Dim Week2RegularTime As Double
            Dim Week2OverTime As Double
            Dim Week2RegularPay As Double
            Dim Week2OvertimePay As Double
    
            REM Calculate the total number of hours for each week
            Dim Week1TotalTime = Week1Monday + Week1Tuesday + Week1Wednesday +
                             Week1Thursday + Week1Friday + Week1Saturday + Week1Sunday
            Dim Week2TotalTime = Week2Monday + Week2Ttuesday + Week2Wednesday +
                             Week2Thursday + Week2Friday + Week2Saturday + Week2Sunday
    
            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 Week1TotalTime < 40 Then
                Week1RegularTime = Week1TotalTime
                Week1RegularPay = HourlySalary * Week1RegularTime
                Week1OverTime = 0.00
                Week1OvertimePay = 0.00
                REM  If the employee worked over 40 hours, calculate the overtime
            ElseIf Week1TotalTime >= 40 Then
                Week1RegularTime = 40
                Week1RegularPay = HourlySalary * 40
                Week1OverTime = Week1TotalTime - 40
                Week1OvertimePay = Week1OverTime * OvertimeSalary
            End If
    
            If Week2TotalTime < 40 Then
                Week2RegularTime = Week2TotalTime
                Week2RegularPay = HourlySalary * Week2RegularTime
                Week2OverTime = 0.00
                Week2OvertimePay = 0.00
            ElseIf Week2TotalTime >= 40 Then
                Week2RegularTime = 40
                Week2RegularPay = HourlySalary * 40
                Week2OverTime = Week2TotalTime - 40
                Week2OvertimePay = Week2OverTime * OvertimeSalary
            End If
    
            Dim WeeklyPay1 = Week1RegularPay + Week1OvertimePay
            Dim WeeklyPay2 = Week2RegularPay + Week2OvertimePay
            Dim NetPay = WeeklyPay1 + WeeklyPay2
    
            TxtWeek1RegularTime.Text = FormatNumber(Week1RegularTime)
            TxtWeek1OverTime.Text = FormatNumber(Week1OverTime)
            TxtWeek1RegularPay.Text = FormatCurrency(Week1RegularPay)
            TxtWeek1OvertimePay.Text = FormatCurrency(Week1OvertimePay)
            TxtWeek1Pay.Text = FormatCurrency(WeeklyPay1)
    
            TxtWeek2RegularTime.Text = FormatNumber(Week2RegularTime)
            TxtWeek2OverTime.Text = FormatNumber(Week2OverTime)
            TxtWeek2RegularPay.Text = FormatCurrency(Week2RegularPay)
            TxtWeek2OvertimePay.Text = FormatCurrency(Week2OvertimePay)
            TxtWeek2Pay.Text = FormatCurrency(WeeklyPay2)
    
            TxtNetPay.Text = FormatCurrency(NetPay)
        End Sub
    End Class
  88. In the Controls combo box, select BtnClose
  89. In the Events combo box, select Click
  90. Implement the Click event as follows:
    Private Sub BtnCloseClick(sender As Object, e As EventArgs) Handles BtnClose.Click
        Close()
    End Sub
  91. To execute the application, on the main manue, click Debug and click Start Without Debugging:

    Payroll Evaluation

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

    Payroll Evaluation

  93. Click the Calculate button:

    Payroll Evaluation

  94. Replace the values as follows:
    Employee Name: Thomas Stones
    Hourly Salary: 31.68
    First Week
        Monday:    8
        Tuesday:   10
        Wednesday: 9
        Thursday:  8.5
        Friday:    8
        Saturday:  0
        Sunday:    0
    Second Week
        Monday:    9
        Tuesday:   8
        Wednesday: 10.5
        Thursday:  9
        Friday:    8.5
        Saturday:  0
        Sunday:    0
  95. Click the Calculate button:

    Payroll Evaluation

  96. Close the form and return to your programming environment
  97. Close your programming environment

Home Copyright © 2010-2026, FunctionX Last Update: Monday 21 September 2026, 15:25 Home