WinForms C# Example Application: Payroll Evaluation - 1 Week
WinForms C# Example Application: Payroll Evaluation - 1 Week
Introduction
We are learning to program in the C# programming language. In this exercise, we will calculate the weekly salary of an employee after getting the time worked for each day of the week. In case a company pays overtime, we will calculate such a salary. Then we will display a summary of the pay in terms of regular pay, overtime pay, and net pay.
Practical Learning: Creating the Application

| Control | Name | Text | Other Properties | |
| GroupBox | Employee Identification | |||
| Label | &Employee Name: | |||
| TextBox | txtEmployeeName | AutoCompleteCustomSource Andy Barang Thomas Stones Gertrude Monay Christophe Yuen Micheline Hammond Paul Bertrand Yamaguchi AutoCompleteMode: Suggest AutoCompleteSource: CustomSource |
||
| Label | Hourly &Salary: | |||
| TextBox | txtHourlySalary | |||
| GroupBox | Time Values | |||
| Label | Monday | |||
| Label | Tuesday | |||
| Label | Wednesday | |||
| Label | Thursday | |||
| Label | Friday | |||
| Label | Saturday | |||
| Label | Sunday | |||
| Label | Work Week: | |||
| TextBox | txtMonday | 0.00 | TextAlign: Right | |
| TextBox | txtTuesday | 0.00 | TextAlign: Right | |
| TextBox | txtWednesday | 0.00 | TextAlign: Right | |
| TextBox | txtThursday | 0.00 | TextAlign: Right | |
| TextBox | txtFriday | 0.00 | TextAlign: Right | |
| TextBox | txtSaturday | 0.00 | TextAlign: Right | |
| TextBox | txtSunday | 0.00 | TextAlign: Right | |
| GroupBox | Payroll Processing | |||
| Label | Hours | |||
| Label | Amount | |||
| Label | btnCalculate | Calculate | ||
| Label | Regular | |||
| TextBox | txtRegularTime | 0.00 | TextAlign: Right Enabled: False |
|
| TextBox | txtRegularAmount | 0.00 | TextAlign: Right Enabled: False |
|
| Label | Net Pay: | |||
| TextBox | txtNetPay | 0.00 | TextAlign: Right Enabled: False |
|
| Label | Overtime | |||
| TextBox | txtOvertime | 0.00 | TextAlign: Right Enabled: False |
|
| TextBox | txtOvertimeAmount | 0.00 | TextAlign: Right Enabled: False |
|
| Label | btnClose | AutoSize: False | ||
namespace PayrollEvaluation1
{
public partial class Exercise : Form
{
public Exercise()
{
InitializeComponent();
}
private void btnCalculate_Click(object sender, EventArgs e)
{
double monday = 0.00, tuesday = 0.00, wednesday = 0.00,
thursday = 0.00, friday = 0.00, saturday = 0.00, sunday = 0.00;
double regularTime = 0.00, overTime = 0.00;
double regularPay = 0.00, overtimePay = 0.00;
double hourlySalary = 0.00;
// Get the hourly salary. Use exception handling in case the user types a bad value.
try
{
hourlySalary = double.Parse(txtHourlySalary.Text);
}
catch (FormatException)
{
MessageBox.Show("The value you typed for the salary is invalid. " +
"Please try again.", "Payroll Evaluation",
MessageBoxButtons.OK, MessageBoxIcon.Information);
txtHourlySalary.Focus();
}
/* Get the value for each work dayworked.
* . Use exception handling for each text box in case the user types a bad value.*/
try
{
monday = double.Parse(txtMonday.Text);
}
catch (FormatException)
{
MessageBox.Show("You typed an invalid value for Monday. " +
"Please try again.", "Payroll Evaluation",
MessageBoxButtons.OK, MessageBoxIcon.Information);
txtMonday.Focus();
}
try
{
tuesday = double.Parse(txtTuesday.Text);
}
catch (FormatException)
{
MessageBox.Show("You typed an invalid value for Tuesday. " +
"Please try again.", "Payroll Evaluation",
MessageBoxButtons.OK, MessageBoxIcon.Information);
txtTuesday.Focus();
}
try
{
wednesday = double.Parse(txtWednesday.Text);
}
catch (FormatException)
{
MessageBox.Show("You typed an invalid value for Wednesday. " +
"Please try again.", "Payroll Evaluation",
MessageBoxButtons.OK, MessageBoxIcon.Information);
txtWednesday.Focus();
}
try
{
thursday = double.Parse(txtThursday.Text);
}
catch (FormatException)
{
MessageBox.Show("You typed an invalid value for Thursday. " +
"Please try again.", "Payroll Evaluation",
MessageBoxButtons.OK, MessageBoxIcon.Information);
txtThursday.Focus();
}
try
{
friday = double.Parse(txtFriday.Text);
}
catch (FormatException)
{
MessageBox.Show("You typed an invalid value for Firday. " +
"Please try again.", "Payroll Evaluation",
MessageBoxButtons.OK, MessageBoxIcon.Information);
txtFriday.Focus();
}
try
{
saturday = double.Parse(txtSaturday.Text);
}
catch (FormatException)
{
MessageBox.Show("You typed an invalid value for Saturday. " +
"Please try again.", "Payroll Evaluation",
MessageBoxButtons.OK, MessageBoxIcon.Information);
txtSaturday.Focus();
}
try
{
sunday = double.Parse(txtSunday.Text);
}
catch (FormatException)
{
MessageBox.Show("You typed an invalid value for Sunday. " +
"Please try again.", "Payroll Evaluation",
MessageBoxButtons.OK, MessageBoxIcon.Information);
txtSunday.Focus();
}
// Calculate the total amount of time the employee worked
double totalTime = monday + tuesday + wednesday +
thursday + friday + saturday + sunday;
// The overtime is paid time and half
double ovtSalary = hourlySalary * 1.5;
// If the employee worked below 40 hours, there is no overtime
if (totalTime < 40)
{
regularTime = totalTime;
regularPay = hourlySalary * regularTime;
overTime = 0.00;
overtimePay = 0.00;
} // If the employee worked over 40 hours, calculate the overtime
else if (totalTime >= 40)
{
regularTime = 40;
regularPay = hourlySalary * 40;
overTime = totalTime - 40;
overtimePay = overTime * ovtSalary;
}
double netPay = regularPay + overtimePay;
txtRegularTime.Text = regularTime.ToString("F");
txtOverTime.Text = overTime.ToString("F");
txtRegularPay.Text = regularPay.ToString("F");
txtOvertimePay.Text = overtimePay.ToString("F");
txtNetPay.Text = netPay.ToString("F");
}
}
}
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}

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


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
|
|
|||
| Home | Copyright © 2010-2026, FunctionX | Thursday 12 December 2024, 09:34 | Home |
|
|
|||