Home

Time Sheet Simulation

Introduction

To create a time sheet for employees of a company, you can use edit boxes in which the users would enter the necessary time. Fortunately, the Microsoft Windows operating system provides a wonderful control that users can easily use to select the time appropriately. This is the role of the Time Picker.

In this application, we will create a time sheet by applying the functionalities of the Time Picker, which is a derivative of the Date Time Picker.

Practical LearningPractical Learning: Using the Time Picker

  1. Create a new project with its default form
  2. Save the project in a new folder called Payroll2
  3. Save the unit as Main and the project as Payroll
  4. Design the form as follows:
      
    Control Name Text/Caption Other Properties
    Form   frmMain Employee Time Sheet  BorderIcons: [biSystemMenu,biMinimize]
    GroupBox GroupBox   Employee Identification   
    Label Label   Employee Name:   
    Edit TextBox edtEmplName     
    GroupBox GroupBox   Time Sheet  
    Label Label   Time In  
    Label Label   Time Out  
    Label Label   Total  
    Label Label   Monday:  
    DateTimePicker DateTimePicker dtpMondayIn   Kind: dtkTime
    DateTimePicker DateTimePicker dtpMondayOut   Kind: dtkTime
    Edit TextBox edtMonday 0.00  
    Label Label   Tuesday:  
    DateTimePicker DateTimePicker dtpTuesdayIn   Kind: dtkTime
    DateTimePicker DateTimePicker dtpTuesdayOut   Kind: dtkTime
    Edit TextBox edtTuesday    
    Label Label   Wednesday:  
    DateTimePicker DateTimePicker dtpWednesdayIn   Kind: dtkTime
    DateTimePicker DateTimePicker dtpWednesdayOut   Kind: dtkTime
    Edit TextBox edtWednesday 0.00  
    Label Label   Thursday:  
    DateTimePicker DateTimePicker dtpThursdayIn   Kind: dtkTime
    DateTimePicker DateTimePicker dtpThursdayOut   Kind: dtkTime
    Edit TextBox edtThursday 0.00  
    Label Label   Friday:  
    DateTimePicker DateTimePicker dtpFridayIn   Kind: dtkTime
    DateTimePicker DateTimePicker dtpFridayOut   Kind: dtkTime
    Edit TextBox edtFriday 0.00  
    Label Label   Saturday:  
    DateTimePicker DateTimePicker dtpSaturdayIn   Kind: dtkTime
    DateTimePicker DateTimePicker dtpSaturdayOut   Kind: dtkTime
    Edit TextBox edtSaturday 0.00  
    Label Label   Sunday:  
    DateTimePicker DateTimePicker dtpSundayIn   Kind: dtkTime
    DateTimePicker DateTimePicker dtpSundayOut   Kind: dtkTime
    Edit TextBox edtSunday 0.00  
    Label Label   Total Time:  
    Edit TextBox edtTotalTime 0.00  
    BitBtn       Kind: bkClose
  5. Display the Class Explorer, right-click TfrmMain and click New Method…
  6. Set the Method Name to CalculateTotalTime
  7. Set the Function Result to void
  8. Make it Private and __fastcall
     
  9. Click OK and implement the method as follows:
      
    //---------------------------------------------------------------------------
    void __fastcall TfrmMain::CalculateTotalTime()
    {
        //TODO: Add your source code here
        double monday, tuesday, wednesday, thursday,
    		   friday, saturday, sunday, totalHours;
    
    	monday     = this->edtMonday->Text.ToDouble();
    	tuesday    = this->edtTuesday->Text.ToDouble();
    	wednesday  = this->edtWednesday->Text.ToDouble();
    	thursday   = this->edtThursday->Text.ToDouble();
    	friday     = this->edtFriday->Text.ToDouble();
    	saturday   = this->edtSaturday->Text.ToDouble();
    	sunday     = this->edtSunday->Text.ToDouble();
    
    	totalHours = monday + tuesday + wednesday + thursday +
    		         friday + saturday + sunday;
    	this->edtTotalTime->Text = FloatToStrF(totalHours, ffFixed, 6, 2);
    }
    //---------------------------------------------------------------------------
  10. Display the Class Explorer again. Right-click TfrmMain and click New Method…
  11. Set the Method Name to CalculateDailyTime
  12. Set the Arguments to TDateTime Start, TDateTime End
  13. Set the Function Result to double
  14. Make it Private and __fastcall
     
  15. Click OK and implement the method as follows:
     
    //---------------------------------------------------------------------------
    double __fastcall TfrmMain::CalculateDailyTime(TDateTime Start, TDateTime End)
    {
        //TODO: Add your source code here
        // Calculate the time difference between both arguments
        TDateTime tmeDiff = End - Start;
    
        // Retrieve the time span values of the difference
         unsigned short hour, minute, second, ms;
         tmeDiff.DecodeTime(&hour, &minute, &second, &ms);
    	 int nbrHours;
      	 double nbrMinutes;
    	 AnsiString strTotalTime;
    
    	 // Calculate the number of minutes in the elapsed time
    	 int minutes = minute + (hour * 60);
    	 // Calculate the number of hours in the elapsed time
      	 nbrHours = minutes / 60;
    
    	 // Convert the time difference to a decimal value
    	 nbrMinutes = (minutes - (nbrHours * 60)) * 5 / 3;
    	 // Format the number to display appropriately
      	 strTotalTime = IntToStr(nbrHours) + "." + FloatToStr(nbrMinutes);
    	 double dblTotalTime = strTotalTime.ToDouble();
    
    	 // Return the result
         return dblTotalTime;
    }
    //---------------------------------------------------------------------------
  16. On the form, click the Date and Time Picker control on the right side of the Monday label to select it
  17. On the Object Inspector, double-click the event side of the OnChange field and implement it as follows:
     
    //---------------------------------------------------------------------------
    void __fastcall TfrmMain::dtpMondayInChange(TObject *Sender)
    {
        // Make sure the time in doesn't occur after the time out
        if( this->dtpMondayIn->Time > this->dtpMondayOut->Time )
            this->dtpMondayIn->Time = this->dtpMondayOut->Time;
    
        // Retrieve the time values of the start and end of the shift
        // Then pass it to the method that can calculate the difference
        double tmeElapsed =
            this->CalculateDailyTime(this->dtpMondayOut->Time,
                                     this->dtpMondayIn->Time);
    
    	 // Display the result in the corresponding text mox
    	 this->edtMonday->Text = FloatToStrF(tmeElapsed, ffFixed, 6, 2);
         this->CalculateTotalTime();
    }
    //---------------------------------------------------------------------------
  18. Return to the form, click the second Date and Time Picker control on the right side of the Monday label to select it
  19. In the Events section of the Object Inspector, double-click the event side of the OnChange field
  20. In the same way, generate an OnChange event for each of the other Time Picker controls from left to right from top down
  21. Implement the events as follows:
     
    //---------------------------------------------------------------------------
    
    #include <vcl.h>
    #pragma hdrstop
    
    #include "Main.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TfrmMain *frmMain;
    //---------------------------------------------------------------------------
    __fastcall TfrmMain::TfrmMain(TComponent* Owner)
        : TForm(Owner)
    {
    }
    //---------------------------------------------------------------------------
    void __fastcall TfrmMain::CalculateTotalTime()
    {
        //TODO: Add your source code here
        double monday, tuesday, wednesday, thursday,
    		   friday, saturday, sunday, totalHours;
    
    	monday     = this->edtMonday->Text.ToDouble();
    	tuesday    = this->edtTuesday->Text.ToDouble();
    	wednesday  = this->edtWednesday->Text.ToDouble();
    	thursday   = this->edtThursday->Text.ToDouble();
    	friday     = this->edtFriday->Text.ToDouble();
    	saturday   = this->edtSaturday->Text.ToDouble();
    	sunday     = this->edtSunday->Text.ToDouble();
    
    	totalHours = monday + tuesday + wednesday + thursday +
    		         friday + saturday + sunday;
    	this->edtTotalTime->Text = FloatToStrF(totalHours, ffFixed, 6, 2);
    }
    //---------------------------------------------------------------------------
    double __fastcall TfrmMain::CalculateDailyTime(TDateTime Start, TDateTime End)
    {
        //TODO: Add your source code here
        // Calculate the time difference between both arguments
        TDateTime tmeDiff = End - Start;
    
        // Retrieve the time span values of the difference
         unsigned short hour, minute, second, ms;
         tmeDiff.DecodeTime(&hour, &minute, &second, &ms);
    	 int nbrHours;
      	 double nbrMinutes;
    	 AnsiString strTotalTime;
    
    	 // Calculate the number of minutes in the elapsed time
    	 int minutes = minute + (hour * 60);
    	 // Calculate the number of hours in the elapsed time
      	 nbrHours = minutes / 60;
    
    	 // Convert the time difference to a decimal value
    	 nbrMinutes = (minutes - (nbrHours * 60)) * 5 / 3;
    	 // Format the number to display appropriately
      	 strTotalTime = IntToStr(nbrHours) + "." + FloatToStr(nbrMinutes);
    	 double dblTotalTime = strTotalTime.ToDouble();
    
    	 // Return the result
         return dblTotalTime;
    }
    //---------------------------------------------------------------------------
    void __fastcall TfrmMain::dtpMondayInChange(TObject *Sender)
    {
        // Make sure the time in doesn't occur after the time out
        if( this->dtpMondayIn->Time > this->dtpMondayOut->Time )
            this->dtpMondayIn->Time = this->dtpMondayOut->Time;
    
        // Retrieve the time values of the start and end of the shift
        // Then pass it to the method that can calculate the difference
        double tmeElapsed =
            this->CalculateDailyTime(this->dtpMondayOut->Time,
                                     this->dtpMondayIn->Time);
    
    	 // Display the result in the corresponding text mox
    	 this->edtMonday->Text = FloatToStrF(tmeElapsed, ffFixed, 6, 2);
         this->CalculateTotalTime();
    }
    //---------------------------------------------------------------------------
    void __fastcall TfrmMain::dtpMondayOutChange(TObject *Sender)
    {
        if( this->dtpMondayOut->Time < this->dtpMondayIn->Time )
            this->dtpMondayOut->Time = this->dtpMondayIn->Time;
    
        double tmeElapsed =
            this->CalculateDailyTime(this->dtpMondayOut->Time,
                                     this->dtpMondayIn->Time);
    
    	 // Display the result in the corresponding text mox
    	 this->edtMonday->Text = FloatToStrF(tmeElapsed, ffFixed, 6, 2);
         this->CalculateTotalTime();
    }
    //---------------------------------------------------------------------------
    void __fastcall TfrmMain::dtpTuesdayInChange(TObject *Sender)
    {
        if( this->dtpTuesdayIn->Time > this->dtpTuesdayOut->Time )
            this->dtpTuesdayIn->Time = this->dtpTuesdayOut->Time;
    
        double tmeElapsed =
            this->CalculateDailyTime(this->dtpTuesdayOut->Time,
                                     this->dtpTuesdayIn->Time);
    
    	 // Display the result in the corresponding text mox
    	 this->edtTuesday->Text = FloatToStrF(tmeElapsed, ffFixed, 6, 2);
         this->CalculateTotalTime();
    }
    //---------------------------------------------------------------------------
    void __fastcall TfrmMain::dtpTuesdayOutChange(TObject *Sender)
    {                   
        if( this->dtpTuesdayOut->Time < this->dtpTuesdayIn->Time )
            this->dtpTuesdayOut->Time = this->dtpTuesdayIn->Time;
    
        double tmeElapsed =
            this->CalculateDailyTime(this->dtpTuesdayOut->Time,
                                     this->dtpTuesdayIn->Time);
    
    	 // Display the result in the corresponding edit control
    	 this->edtTuesday->Text = FloatToStrF(tmeElapsed, ffFixed, 6, 2);
         this->CalculateTotalTime();
    }
    //---------------------------------------------------------------------------
    void __fastcall TfrmMain::dtpWednesdayInChange(TObject *Sender)
    {
        if( this->dtpWednesdayIn->Time > this->dtpWednesdayOut->Time )
            this->dtpWednesdayIn->Time = this->dtpWednesdayOut->Time;
    
        double tmeElapsed =
            this->CalculateDailyTime(this->dtpWednesdayOut->Time,
                                     this->dtpWednesdayIn->Time);
    
    	 // Display the result in the corresponding text mox
    	 this->edtWednesday->Text = FloatToStrF(tmeElapsed, ffFixed, 6, 2);
         this->CalculateTotalTime();
    }
    //---------------------------------------------------------------------------
    void __fastcall TfrmMain::dtpWednesdayOutChange(TObject *Sender)
    {                    
        if( this->dtpWednesdayOut->Time < this->dtpWednesdayIn->Time )
            this->dtpWednesdayOut->Time = this->dtpWednesdayIn->Time;
    
        double tmeElapsed =
            this->CalculateDailyTime(this->dtpWednesdayOut->Time,
                                     this->dtpWednesdayIn->Time);
    
    	 this->edtWednesday->Text = FloatToStrF(tmeElapsed, ffFixed, 6, 2);
         this->CalculateTotalTime();
    }
    //---------------------------------------------------------------------------
    void __fastcall TfrmMain::dtpThursdayInChange(TObject *Sender)
    {
        if( this->dtpThursdayIn->Time > this->dtpThursdayOut->Time )
            this->dtpThursdayIn->Time = this->dtpThursdayOut->Time;
    
        double tmeElapsed =
            this->CalculateDailyTime(this->dtpThursdayOut->Time,
                                     this->dtpThursdayIn->Time);
    
    	 // Display the result in the corresponding text mox
    	 this->edtThursday->Text = FloatToStrF(tmeElapsed, ffFixed, 6, 2);
         this->CalculateTotalTime();
    }
    //---------------------------------------------------------------------------
    void __fastcall TfrmMain::dtpThursdayOutChange(TObject *Sender)
    {          
        if( this->dtpThursdayOut->Time < this->dtpThursdayIn->Time )
            this->dtpThursdayOut->Time = this->dtpThursdayIn->Time;
    
        double tmeElapsed =
            this->CalculateDailyTime(this->dtpThursdayOut->Time,
                                     this->dtpThursdayIn->Time);
    
    	 // Display the result in the corresponding edit control
    	 this->edtThursday->Text = FloatToStrF(tmeElapsed, ffFixed, 6, 2);
         this->CalculateTotalTime();
    }
    //---------------------------------------------------------------------------
    void __fastcall TfrmMain::dtpFridayInChange(TObject *Sender)
    {
        if( this->dtpFridayIn->Time > this->dtpFridayOut->Time )
            this->dtpFridayIn->Time = this->dtpFridayOut->Time;
    
        double tmeElapsed =
            this->CalculateDailyTime(this->dtpFridayOut->Time,
                                     this->dtpFridayIn->Time);
    
    	 // Display the result in the corresponding text mox
    	 this->edtFriday->Text = FloatToStrF(tmeElapsed, ffFixed, 6, 2);
         this->CalculateTotalTime();
    }
    //---------------------------------------------------------------------------
    void __fastcall TfrmMain::dtpFridayOutChange(TObject *Sender)
    {                                         
        if( this->dtpFridayOut->Time < this->dtpFridayIn->Time )
            this->dtpFridayOut->Time = this->dtpFridayIn->Time;
    
        double tmeElapsed =
            this->CalculateDailyTime(this->dtpFridayOut->Time,
                                     this->dtpFridayIn->Time);
    
    	 // Display the result in the corresponding edit control
    	 this->edtFriday->Text = FloatToStrF(tmeElapsed, ffFixed, 6, 2);
         this->CalculateTotalTime();
    }
    //---------------------------------------------------------------------------
    void __fastcall TfrmMain::dtpSaturdayInChange(TObject *Sender)
    {
        if( this->dtpSaturdayIn->Time > this->dtpSaturdayOut->Time )
            this->dtpSaturdayIn->Time = this->dtpSaturdayOut->Time;
    
        double tmeElapsed =
            this->CalculateDailyTime(this->dtpSaturdayOut->Time,
                                     this->dtpSaturdayIn->Time);
    
    	 // Display the result in the corresponding edit control
    	 this->edtSaturday->Text = FloatToStrF(tmeElapsed, ffFixed, 6, 2);
         this->CalculateTotalTime();
    }
    //---------------------------------------------------------------------------
    void __fastcall TfrmMain::dtpSaturdayOutChange(TObject *Sender)
    {                       
        if( this->dtpSaturdayOut->Time < this->dtpSaturdayIn->Time )
            this->dtpSaturdayOut->Time = this->dtpSaturdayIn->Time;
    
        double tmeElapsed =
            this->CalculateDailyTime(this->dtpSaturdayOut->Time,
                                     this->dtpSaturdayIn->Time);
    
    	 // Display the result in the corresponding edit control
    	 this->edtSaturday->Text = FloatToStrF(tmeElapsed, ffFixed, 6, 2);
         this->CalculateTotalTime();
    }
    //---------------------------------------------------------------------------
    void __fastcall TfrmMain::dtpSundayInChange(TObject *Sender)
    {
        if( this->dtpSundayIn->Time > this->dtpSundayOut->Time )
            this->dtpSundayIn->Time = this->dtpSundayOut->Time;
    
        double tmeElapsed =
            this->CalculateDailyTime(this->dtpSundayOut->Time,
                                     this->dtpSundayIn->Time);
    
    	 // Display the result in the corresponding edit control
    	 this->edtSunday->Text = FloatToStrF(tmeElapsed, ffFixed, 6, 2);
         this->CalculateTotalTime();
    }
    //---------------------------------------------------------------------------
    void __fastcall TfrmMain::dtpSundayOutChange(TObject *Sender)
    {                            
        if( this->dtpSundayOut->Time < this->dtpSundayIn->Time )
            this->dtpSundayOut->Time = this->dtpSundayIn->Time;
    
        double tmeElapsed =
            this->CalculateDailyTime(this->dtpSundayOut->Time,
                                     this->dtpSundayIn->Time);
    
    	 // Display the result in the corresponding edit control
    	 this->edtSunday->Text = FloatToStrF(tmeElapsed, ffFixed, 6, 2);
         this->CalculateTotalTime();
    }
    //---------------------------------------------------------------------------
  22. Execute the application to test it
 

Home Copyright © 2005-2012, FunctionX, Inc.