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 PayrollEvaluation1
  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="PayrollEvaluation1.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:PayrollEvaluation1"
            mc:Ignorable="d"
            Title="Payroll Evaluation" Height="280" Width="540">
        <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:" HorizontalAlignment="Left" Margin="77,99,0,0" VerticalAlignment="Top" RenderTransformOrigin="1.801,0.33"/>
            <TextBox x:Name="txtMonday" HorizontalAlignment="Left" Margin="124,103,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="64"/>
            <TextBox x:Name="txtTuesday" HorizontalAlignment="Left" Margin="203,103,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="64"/>
            <TextBox x:Name="txtWednesday" HorizontalAlignment="Left" Margin="287,103,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="64"/>
            <TextBox x:Name="txtThursday" HorizontalAlignment="Left" Margin="370,102,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="64"/>
            <TextBox x:Name="txtFriday" HorizontalAlignment="Left" Margin="449,103,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="64" RenderTransformOrigin="0.433,3.12"/>
            <Label Content="Calculation __________________________________________________________________________________________________________________________________________________________________________" HorizontalAlignment="Center" Margin="0,125,0,0" VerticalAlignment="Top" Width="503"/>
            <Label Content="Time" HorizontalAlignment="Left" Margin="207,146,0,0" VerticalAlignment="Top" Cursor=""/>
            <Label Content="Pay" HorizontalAlignment="Left" Margin="281,146,0,0" VerticalAlignment="Top" Cursor=""/>
            <Button x:Name="btnCalculate" Content="Calculate" HorizontalAlignment="Left" Margin="22,172,0,0" VerticalAlignment="Top" Width="92" RenderTransformOrigin="0.554,1.701" Height="47"/>
            <Label Content="Regular:" HorizontalAlignment="Left" Margin="141,168,0,0" VerticalAlignment="Top"/>
            <TextBox x:Name="txtRegularTime" HorizontalAlignment="Left" Margin="207,172,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="64"/>
            <TextBox x:Name="txtRegularPay" HorizontalAlignment="Left" Margin="286,172,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="64"/>
            <Label Content="Overtime:" HorizontalAlignment="Left" Margin="141,197,0,0" VerticalAlignment="Top"/>
            <TextBox x:Name="txtOverTime" HorizontalAlignment="Left" Margin="207,201,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="64"/>
            <TextBox x:Name="txtOvertimePay" HorizontalAlignment="Left" Margin="286,201,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="64"/>
            <Label Content="Net Pay:" HorizontalAlignment="Left" Margin="391,197,0,0" VerticalAlignment="Top"/>
            <TextBox x:Name="txtNetPay" HorizontalAlignment="Left" Margin="449,201,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 PayrollEvaluation1
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void btnCalculate_Click(object sender, RoutedEventArgs 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;
    
    
                try
                {
                    hourlySalary = double.Parse(txtHourlySalary.Text);
                }
                catch (FormatException fex)
                {
                    MessageBox.Show("The value for the hourly salary could not be retrieved. " +
                                    "The error produced was:" + Environment.NewLine +
                                    fex.Message, "Payroll Evaluation",
                                    MessageBoxButton.OK, MessageBoxImage.Information);
                }
    
                /* 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",
                                    MessageBoxButton.OK, MessageBoxImage.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",
                                    MessageBoxButton.OK, MessageBoxImage.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",
                                    MessageBoxButton.OK, MessageBoxImage.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",
                                    MessageBoxButton.OK, MessageBoxImage.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",
                                    MessageBoxButton.OK, MessageBoxImage.Information);
                    txtFriday.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");
            }
        }
    }
  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
    Work Week
        Monday:    8
        Tuesday:   7.5
        Wednesday: 6
        Thursday:  7.5
        Friday:    6.5

    Payroll Evaluation

  12. Click the Calculate button:

    Payroll Evaluation

  13. Replace the values as follows:
    Employee Name: July Sanders
    Hourly Salary: 31.68
    Work Week
        Monday:    8
        Tuesday:   10.5
        Wednesday: 9
        Thursday:  8
        Friday:    8
  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