Introduction

We are learning to create WPF (Windows Presentation Foundation) application. In this introduction lesson, we will calculate the weekly salary of an employee after getting the time worked for each day of a week.

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 C# and, in the list of project templates, click Windows Forms App
  4. Click Next
  5. In the Configure Your New Project dialog box, set the Project Name to PayrollEvaluation2
  6. Click Next
  7. In the Additional Information dialog box, in the Framework combo box, select the highest version (.NET 9.0 (Standard Term Support)).
    Click Create
  8. In the MainWindow.xaml file, change the XAML code as follows:

    Payroll Evaluatiion

    <Window x:Class="PayrollEvaluation2.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:PayrollEvaluation2"
            mc:Ignorable="d"
            Title="Payroll Evaluation" Height="350" Width="560">
        <Grid>
            <Label Content="Employee Name:" HorizontalAlignment="Left" Margin="20,20,0,0" VerticalAlignment="Top" Cursor=""/>
            <TextBox x:Name="txtEmployeeName" HorizontalAlignment="Left" Margin="124,24,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120"/>
            <Label Content="Hourly Salary:" HorizontalAlignment="Left" Margin="282,20,0,0" VerticalAlignment="Top" Cursor=""/>
            <TextBox x:Name="txtHourlySalary" HorizontalAlignment="Left" Margin="372,25,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="62" RenderTransformOrigin="1.05,0.664"/>
            <Label Content="Time Worked ______________________________________________________________________________________________________________________________________________________________________" HorizontalAlignment="Center" Margin="0,51,0,0" VerticalAlignment="Top" Width="503"/>
            <Label Content="Monday" HorizontalAlignment="Left" Margin="124,77,0,0" VerticalAlignment="Top" Cursor=""/>
            <Label Content="Tuesday" HorizontalAlignment="Left" Margin="198,77,0,0" VerticalAlignment="Top" Cursor=""/>
            <Label Content="Wednesday" HorizontalAlignment="Left" Margin="282,77,0,0" VerticalAlignment="Top" Cursor=""/>
            <Label Content="Thursday" HorizontalAlignment="Left" Margin="372,77,0,0" VerticalAlignment="Top" Cursor=""/>
            <Label Content="Friday" HorizontalAlignment="Left" Margin="449,77,0,0" VerticalAlignment="Top" Cursor=""/>
            <Label Content="Week 1:" HorizontalAlignment="Left" Margin="53,104,0,0" VerticalAlignment="Top" RenderTransformOrigin="1.801,0.33"/>
            <TextBox x:Name="txtWeek1Monday" HorizontalAlignment="Left" Margin="124,108,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="64"/>
            <TextBox x:Name="txtWeek1Tuesday" HorizontalAlignment="Left" Margin="203,108,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="64"/>
            <TextBox x:Name="txtWeek1Wednesday" HorizontalAlignment="Left" Margin="287,108,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="64"/>
            <TextBox x:Name="txtWeek1Thursday" HorizontalAlignment="Left" Margin="370,107,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="64"/>
            <TextBox x:Name="txtWeek1Friday" HorizontalAlignment="Left" Margin="449,108,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="64" RenderTransformOrigin="0.433,3.12"/>
            <Label Content="Week 2:" HorizontalAlignment="Left" Margin="53,138,0,0" VerticalAlignment="Top" RenderTransformOrigin="1.801,0.33"/>
            <TextBox x:Name="txtWeek2Monday" HorizontalAlignment="Left" Margin="124,142,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="64"/>
            <TextBox x:Name="txtWeek2Tuesday" HorizontalAlignment="Left" Margin="203,142,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="64"/>
            <TextBox x:Name="txtWeek2Wednesday" HorizontalAlignment="Left" Margin="287,142,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="64"/>
            <TextBox x:Name="txtWeek2Thursday" HorizontalAlignment="Left" Margin="370,141,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="64"/>
            <TextBox x:Name="txtWeek2Friday" HorizontalAlignment="Left" Margin="449,142,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="64" RenderTransformOrigin="0.433,3.12"/>
            <Label Content="Calculation __________________________________________________________________________________________________________________________________________________________________________" HorizontalAlignment="Center" Margin="0,167,0,0" VerticalAlignment="Top" Width="504"/>
            <Label Content="Time" HorizontalAlignment="Left" Margin="207,196,0,0" VerticalAlignment="Top" Cursor=""/>
            <Label Content="Pay" HorizontalAlignment="Left" Margin="281,196,0,0" VerticalAlignment="Top" Cursor=""/>
            <Button x:Name="btnCalculate" Content="Calculate" HorizontalAlignment="Left" Margin="22,222,0,0" VerticalAlignment="Top" Width="92" RenderTransformOrigin="0.554,1.701" Height="47" />
            <Label Content="Regular:" HorizontalAlignment="Left" Margin="141,218,0,0" VerticalAlignment="Top"/>
            <TextBox x:Name="txtRegularTime" HorizontalAlignment="Left" Margin="207,222,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="64"/>
            <TextBox x:Name="txtRegularPay" HorizontalAlignment="Left" Margin="286,222,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="64"/>
            <Label Content="Overtime:" HorizontalAlignment="Left" Margin="141,247,0,0" VerticalAlignment="Top"/>
            <TextBox x:Name="txtOverTime" HorizontalAlignment="Left" Margin="207,251,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="64"/>
            <TextBox x:Name="txtOvertimePay" HorizontalAlignment="Left" Margin="286,251,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="64"/>
            <Label Content="Net Pay:" HorizontalAlignment="Left" Margin="391,247,0,0" VerticalAlignment="Top"/>
            <TextBox x:Name="txtNetPay" HorizontalAlignment="Left" Margin="449,251,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="64"/>
        </Grid>
    </Window>
  9. On the form designer, double-click the Calculate button and implement its event as follows:
    using System.Windows;
    
    namespace PayrollEvaluation2
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void btnCalculate_Click(object sender, RoutedEventArgs e)
            {
                double week1Monday = 0.00, week1Tuesday = 0.00, week1Wednesday = 0.00,
                       week1Thursday = 0.00, week1Friday = 0.00, week1Saturday = 0.00,
                       week1Sunday = 0.00, week2Monday = 0.00, week2Ttuesday = 0.00,
                       week2Wednesday = 0.00, week2Thursday = 0.00, week2Friday = 0.00,
                       week2Saturday = 0.00, week2Sunday = 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",
                                    MessageBoxButton.OK, MessageBoxImage.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
                {
                    week1Monday = double.Parse(txtWeek1Monday.Text);
                }
                catch (FormatException)
                {
                    MessageBox.Show("You typed an invalid value for Monday of the first week. " +
                                    "Please try again.", "Payroll Evaluation",
                                    MessageBoxButton.OK, MessageBoxImage.Information);
                    txtWeek1Monday.Focus();
                }
                try
                {
                    week1Tuesday = double.Parse(txtWeek1Tuesday.Text);
                }
                catch (FormatException)
                {
                    MessageBox.Show("You typed an invalid value for Tuesday of the first week. " +
                                    "Please try again.", "Payroll Evaluation",
                                    MessageBoxButton.OK, MessageBoxImage.Information);
                    this.txtWeek1Tuesday.Focus();
                }
                try
                {
                    week1Wednesday = double.Parse(txtWeek1Wednesday.Text);
                }
                catch (FormatException)
                {
                    MessageBox.Show("You typed an invalid value for Wednesday of the first week. " +
                                    "Please try again.", "Payroll Evaluation",
                                    MessageBoxButton.OK, MessageBoxImage.Information);
                    txtWeek1Wednesday.Focus();
                }
                try
                {
                    week1Thursday = double.Parse(txtWeek1Thursday.Text);
                }
                catch (FormatException)
                {
                    MessageBox.Show("You typed an invalid value for Thursday of the first week. " +
                                    "Please try again.", "Payroll Evaluation",
                                    MessageBoxButton.OK, MessageBoxImage.Information);
                    txtWeek1Thursday.Focus();
                }
    
                try
                {
                    week1Friday = double.Parse(txtWeek1Friday.Text);
                }
                catch (FormatException)
                {
                    MessageBox.Show("You typed an invalid value for Firday of the first week. " +
                                    "Please try again.", "Payroll Evaluation",
                                    MessageBoxButton.OK, MessageBoxImage.Information);
                    txtWeek1Friday.Focus();
                }
    
                try
                {
                    week2Monday = double.Parse(txtWeek2Monday.Text);
                }
                catch (FormatException)
                {
                    MessageBox.Show("You typed an invalid value for Monday of the second week. " +
                                    "Please try again.", "Payroll Evaluation",
                                    MessageBoxButton.OK, MessageBoxImage.Information);
                    txtWeek2Monday.Focus();
                }
                try
                {
                    week2Ttuesday = double.Parse(txtWeek2Tuesday.Text);
                }
                catch (FormatException)
                {
                    MessageBox.Show("You typed an invalid value for Tuesday of the second week. " +
                                    "Please try again.", "Payroll Evaluation",
                                    MessageBoxButton.OK, MessageBoxImage.Information);
                    txtWeek2Tuesday.Focus();
                }
                try
                {
                    week2Wednesday = double.Parse(txtWeek2Wednesday.Text);
                }
                catch (FormatException)
                {
                    MessageBox.Show("You typed an invalid value for Wednesday of the second week. " +
                                    "Please try again.", "Payroll Evaluation",
                                    MessageBoxButton.OK, MessageBoxImage.Information);
                    txtWeek2Wednesday.Focus();
                }
                try
                {
                    week2Thursday = double.Parse(txtWeek2Thursday.Text);
                }
                catch (FormatException)
                {
                    MessageBox.Show("You typed an invalid value for Thursday of the second week. " +
                                    "Please try again.", "Payroll Evaluation",
                                    MessageBoxButton.OK, MessageBoxImage.Information);
                    txtWeek2Thursday.Focus();
                }
                try
                {
                    week2Friday = double.Parse(txtWeek2Friday.Text);
                }
                catch (FormatException)
                {
                    MessageBox.Show("You typed an invalid value for Friday of the second week. " +
                                    "Please try again.", "Payroll Evaluation",
                                    MessageBoxButton.OK, MessageBoxImage.Information);
                    txtWeek2Friday.Focus();
                }
    
                // Calculate the total number of hours for each week
                double week1TotalTime = week1Monday + week1Tuesday + week1Wednesday +
                                        week1Thursday + week1Friday + week1Saturday + week1Sunday;
                double week2TotalTime = week2Monday + week2Ttuesday + week2Wednesday +
                                        week2Thursday + week2Friday + week2Saturday + week2Sunday;
    
                // The overtime is paid time and half
                double ovtSalary = hourlySalary * 1.5;
    
                double week1RegularTime = 0.00, week2RegularTime = 0.00,
                       week1OverTime = 0.00, week2OverTime = 0.00;
                double week1RegularPay = 0.00, week2RegularPay = 0.00,
                       week1OvertimePay = 0.00, week2OvertimePay = 0.00;
    
                // If the employee worked below 40 hours, there is no overtime
                if (week1TotalTime < 40)
                {
                    week1RegularTime = week1TotalTime;
                    week1RegularPay = hourlySalary * week1RegularTime;
                    week1OverTime = 0.00;
                    week1OvertimePay = 0.00;
                } // If the employee worked over 40 hours, calculate the overtime
                else if (week1TotalTime >= 40)
                {
                    week1RegularTime = 40;
                    week1RegularPay = hourlySalary * 40;
                    week1OverTime = week1TotalTime - 40;
                    week1OvertimePay = week1OverTime * ovtSalary;
                }
    
                if (week2TotalTime < 40)
                {
                    week2RegularTime = week2TotalTime;
                    week2RegularPay = hourlySalary * week2RegularTime;
                    week2OverTime = 0.00;
                    week2OvertimePay = 0.00;
                }
                else if (week2TotalTime >= 40)
                {
                    week2RegularTime = 40;
                    week2RegularPay = hourlySalary * 40;
                    week2OverTime = week2TotalTime - 40;
                    week2OvertimePay = week2OverTime * ovtSalary;
                }
    
                double regularTime = week1RegularTime + week2RegularTime;
                double overTime = week1OverTime + week2OverTime;
                double regularPay = week1RegularPay + week2RegularPay;
                double overtimePay = week1OvertimePay + week2OvertimePay;
    
                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");
            }
        }
    }
  10. To execute the application, on the main menu of Microsoft Visual Studio, click Debug and click Start Without Debugging:

    Payroll Evaluation

  11. Enter the following values in the text boxes:
    Employee Name: Gertrude Monay
    Hourly Salary: 28.46
    Week 1
        Monday:    8
        Tuesday:   7.5
        Wednesday: 6
        Thursday:  7.5
        Friday:    6.5
    Week 2
        Monday:    7
        Tuesday:   8
        Wednesday: 6
        Thursday:  6
        Friday:    8

    Payroll Evaluation

  12. Click the Calculate button:

    Payroll Evaluation

  13. Replace the values as follows:
    Employee Name: July Sanders
    Hourly Salary: 31.68
    Week 2
        Monday:    8
        Tuesday:   10.5
        Wednesday: 9
        Thursday:  8
        Friday:    8.5
    Week 2
        Monday:    9
        Tuesday:   8
        Wednesday: 10
        Thursday:  8
        Friday:    10.5
  14. Click the Calculate button:

    Payroll Evaluation

  15. Close the form and return to your programming environment

Home Copyright © 2010-2025, FunctionX Friday 04 July 2025, 10:14 Home