Home

Time Sheet Simulation

Introduction

The Microsoft Windows operating system provides a formatted control that can be used to set or change a time value. This control is from the Date Time Picker. You can see an example of this control if you double-click the time on the taskbar. The Time Picker control allows the user to change the hour value, the minute value, the second value or the AM/PM attribute.

In this exercise, we will use the Time Picker to simulate a time sheet with one shift.

Practical LearningPractical Learning: Using the Time Picker

  1.  Start a new Windows Forms Application named TimeSheet1
  2. Set the form's Icon to Drvie:\Program Files\Microsoft Visual Studio .NET 2003\Common7\Graphics\icons\Office\FOLDER05.ICO
  3. Design the form as follows:
     
    Bethesda Car Rental - Employee Time Sheet - Form Design
    Control Name Text Other Properties
    GroupBox GroupBox   Employee Identification  
    Label Label   Employee #:  
    TextBox TextBox txtEmployeeNbr    
    TextBox TextBox txtEmployeeName    
    GroupBox GroupBox   Time Card  
    Label Label   Time In  
    Label Label   Tim Out  
    Label Label   Hours  
    Label Label   Monday:  
    GroupBox GroupBox   Time Sheet  
    DateTimePicker DateTimePicker dtpMondayIn   Format: Time
    ShowUpDown: True
    DateTimePicker DateTimePicker dtpMondayOut   Format: Time
    ShowUpDown: True
    TextBox TextBox txtMonday 0.00 TextAlign: Right
    Label Label   Tuesday:  
    DateTimePicker DateTimePicker dtpTuesdayIn   Format: Time
    ShowUpDown: True
    DateTimePicker DateTimePicker dtpTuesdayOut   Format: Time
    ShowUpDown: True
    TextBox TextBox txtTuesday 0.00 TextAlign: Right
    Label Label   Wednesday:  
    DateTimePicker DateTimePicker dtpWednesdayIn   Format: Time
    ShowUpDown: True
    DateTimePicker DateTimePicker dtpWednesdayOut   Format: Time
    ShowUpDown: True
    TextBox TextBox txtWednesday 0.00 TextAlign: Right
    Label Label   Thursday:  
    DateTimePicker DateTimePicker dtpThursdayIn   Format: Time
    ShowUpDown: True
    DateTimePicker DateTimePicker dtpThursdayOut   Format: Time
    ShowUpDown: True
    TextBox TextBox txtThursday 0.00 TextAlign: Right
    Label Label   Friday:  
    DateTimePicker DateTimePicker dtpFridayIn   Format: Time
    ShowUpDown: True
    DateTimePicker DateTimePicker dtpFridayOut   Format: Time
    ShowUpDown: True
    TextBox TextBox txtFriday 0.00 TextAlign: Right
    Label Label   Saturday:  
    DateTimePicker DateTimePicker dtpSaturdayIn   Format: Time
    ShowUpDown: True
    DateTimePicker DateTimePicker dtpSaturdayOut   Format: Time
    ShowUpDown: True
    TextBox TextBox txtSaturday 0.00 TextAlign: Right
    Label Label   Sunday:  
    DateTimePicker DateTimePicker dtpSundayIn   Format: Time
    ShowUpDown: True
    DateTimePicker DateTimePicker dtpSundayOut   Format: Time
    ShowUpDown: True
    TextBox TextBox txtSunday 0.00 TextAlign: Right
    GroupBox GroupBox   Time Sheet Result  
    Button Button btnClose Close  
    Label Label   Total Hours:  
    TextBox TextBox txtTotalHours 0.00 TextAlign: Right
  4. Double-click the Close button and implement its Click event as follows:
     
    System::Void btnClose_Click(System::Object *  sender, System::EventArgs *  e)
    {
    	 Close();
    }
  5. Execute it to test it and then close the form

Double-click an unoccupied are of the form to access its Load event and implement it as follows:
 

System::Void Form1_Load(System::Object *  sender, System::EventArgs *  e)
{
	 DateTime TimeToSet = DateTime(DateTime::Now.Year, DateTime::Now.Month, DateTime::Now.Day, 9, 0, 0);

	 this->dtpMondayIn->Value     = TimeToSet;
	 this->dtpMondayOut->Value    = TimeToSet;
	 this->dtpTuesdayIn->Value    = TimeToSet;
	 this->dtpTuesdayOut->Value   = TimeToSet;
	 this->dtpWednesdayIn->Value  = TimeToSet;
	 this->dtpWednesdayOut->Value = TimeToSet;
	 this->dtpThursdayIn->Value   = TimeToSet;
	 this->dtpThursdayOut->Value  = TimeToSet;
	 this->dtpFridayIn->Value     = TimeToSet;
	 this->dtpFridayOut->Value    = TimeToSet;
	 this->dtpSaturdayIn->Value   = TimeToSet;
	 this->dtpSaturdayOut->Value  = TimeToSet;
	 this->dtpSundayIn->Value     = TimeToSet;
	 this->dtpSundayOut->Value    = TimeToSet;
}
  1. To create a method that can calculate the total hours, in Class View, right-click Form1 -> Add -> Add Function...
  2. Set the Return Type to void
  3. Set the Function Name to CalculateWeeklyHours
  4. Set the Access level to private
     
  5. Click Finish
  6. Implement the function as follows:
     
    void CalculateWeeklyHours(void)
    {
    	double monday, tuesday, wednesday, thursday,
    		   friday, saturday, sunday, totalHours;
    
    	monday     = this->txtMonday->Text->ToDouble(0);
    	tuesday    = this->txtTuesday->Text->ToDouble(0);
    	wednesday  = this->txtWednesday->Text->ToDouble(0);
    	thursday   = this->txtThursday->Text->ToDouble(0);
    	friday     = this->txtFriday->Text->ToDouble(0);
    	saturday   = this->txtSaturday->Text->ToDouble(0);
    	sunday     = this->txtSunday->Text->ToDouble(0);
    
    	totalHours = monday + tuesday + wednesday + thursday +
    		         friday + saturday + sunday;
    	this->txtTotalHours->Text = totalHours.ToString(S"F");
    }
  7. Display the form
    On the form, double-click the left Date Time Picker control of Monday just under the Time In label
  8. Implement the function as follows:
     
    System::Void dtpMondayIn_ValueChanged(System::Object *  sender, System::EventArgs *  e)
    {
    	 // Make sure the time in doesn't occur AFTER the time out
    	 if( this->dtpMondayIn->Value > this->dtpMondayOut->Value )
    		 this->dtpMondayIn->Value = this->dtpMondayOut->Value;
    
    	 // Calculate the time difference between both ends of the shift
    	 System::TimeSpan diff = this->dtpMondayOut->Value.Subtract(this->dtpMondayIn->Value);
    			 
    	 int nbrHours;
    	 double nbrMinutes;
    	 String *strTotalTime = 0;
    
    	 // Calculate the number of minutes in the elapsed time
    	 int minutes = diff.Minutes + (diff.Hours * 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 = String::Concat(nbrHours.ToString(), S".", nbrMinutes.ToString());
    
    	 // Display the result in the corresponding text mox
    	 this->txtMonday->Text = strTotalTime;
    	CalculateWeeklyHours();
    }
  9. Display the form
    On the form, double-click the right Date Time Picker control of Monday just under the Time Out label
  10. Return to the form and double-click each of the Date Time Pickers from left to right and from top to bottom
  11. Implement the events as follows:
     
    System::Void dtpMondayIn_ValueChanged(System::Object *  sender, System::EventArgs *  e)
    {
    	 // Make sure the time in doesn't occur AFTER the time out
    	 if( this->dtpMondayIn->Value > this->dtpMondayOut->Value )
    		 this->dtpMondayIn->Value = this->dtpMondayOut->Value;
    
    	 // Calculate the time difference between both ends of the shift
    	 System::TimeSpan diff = this->dtpMondayOut->Value.Subtract(this->dtpMondayIn->Value);
    			 
    	 int nbrHours;
    	 double nbrMinutes;
    	 String *strTotalTime = 0;
    
    	 // Calculate the number of minutes in the elapsed time
    	 int minutes = diff.Minutes + (diff.Hours * 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 = String::Concat(nbrHours.ToString(), S".", nbrMinutes.ToString());
    
    	 // Display the result in the corresponding text mox
    	 this->txtMonday->Text = strTotalTime;
    	CalculateWeeklyHours();
    }
    
    private: System::Void dtpMondayOut_ValueChanged(System::Object *  sender, System::EventArgs *  e)
    {
    	 if( this->dtpMondayOut->Value < this->dtpMondayIn->Value )
    		 this->dtpMondayOut->Value = this->dtpMondayIn->Value;
    		 
    	 System::TimeSpan diff = this->dtpMondayOut->Value.Subtract(this->dtpMondayIn->Value);
    			 
    	 int nbrHours;
    	 double nbrMinutes;
    	 String *strTotalTime = 0;
    
    	 int minutes = diff.Minutes + (diff.Hours * 60);
    	 nbrHours = minutes / 60;
    	 nbrMinutes = (minutes - (nbrHours * 60)) * 5 / 3;
    	 strTotalTime = String::Concat(nbrHours.ToString(), S".", nbrMinutes.ToString());
    
    	 this->txtMonday->Text = strTotalTime;
    	CalculateWeeklyHours();
    }
    
    private: System::Void dtpTuesdayIn_ValueChanged(System::Object *  sender, System::EventArgs *  e)
    {
    	 if( this->dtpTuesdayIn->Value > this->dtpTuesdayOut->Value )
    		 this->dtpTuesdayIn->Value = this->dtpTuesdayOut->Value;
    
    	 System::TimeSpan diff = this->dtpTuesdayOut->Value.Subtract(this->dtpTuesdayIn->Value);
    			 
    	 int nbrHours;
    	 double nbrMinutes;
    	 String *strTotalTime = 0;
    
    	 int minutes = diff.Minutes + (diff.Hours * 60);
    	 nbrHours = minutes / 60;
    	 nbrMinutes = (minutes - (nbrHours * 60)) * 5 / 3;
    	 strTotalTime = String::Concat(nbrHours.ToString(), S".", nbrMinutes.ToString());
    
    	 this->txtTuesday->Text = strTotalTime;
    	CalculateWeeklyHours();
    }
    
    private: System::Void dtpTuesdayOut_ValueChanged(System::Object *  sender, System::EventArgs *  e)
    {
    	 if( this->dtpTuesdayOut->Value < this->dtpTuesdayIn->Value )
    		 this->dtpTuesdayOut->Value = this->dtpTuesdayIn->Value;
    			 
    	 System::TimeSpan diff = this->dtpTuesdayOut->Value.Subtract(this->dtpTuesdayIn->Value);
    			 
    	 int nbrHours;
    	 double nbrMinutes;
    	 String *strTotalTime = 0;
    
    	 int minutes = diff.Minutes + (diff.Hours * 60);
    	 nbrHours = minutes / 60;
    	 nbrMinutes = (minutes - (nbrHours * 60)) * 5 / 3;
    	 strTotalTime = String::Concat(nbrHours.ToString(), S".", nbrMinutes.ToString());
    
    	 this->txtTuesday->Text = strTotalTime;
    	CalculateWeeklyHours();
    }
    
    private: System::Void dtpWednesdayIn_ValueChanged(System::Object *  sender, System::EventArgs *  e)
    {
    	 if( this->dtpWednesdayIn->Value > this->dtpWednesdayOut->Value )
    		 this->dtpWednesdayIn->Value = this->dtpWednesdayOut->Value;
    
    	 System::TimeSpan diff = this->dtpWednesdayOut->Value.Subtract(this->dtpWednesdayIn->Value);
    			 
    	 int nbrHours;
    	 double nbrMinutes;
    	 String *strTotalTime = 0;
    
    	 int minutes = diff.Minutes + (diff.Hours * 60);
    	 nbrHours = minutes / 60;
    	 nbrMinutes = (minutes - (nbrHours * 60)) * 5 / 3;
    	 strTotalTime = String::Concat(nbrHours.ToString(), S".", nbrMinutes.ToString());
    
    	 this->txtWednesday->Text = strTotalTime;
    	CalculateWeeklyHours();
    }
    
    private: System::Void dtpWednesdayOut_ValueChanged(System::Object *  sender, System::EventArgs *  e)
    {
    	 if( this->dtpWednesdayOut->Value < this->dtpWednesdayIn->Value )
    		 this->dtpWednesdayOut->Value = this->dtpWednesdayIn->Value;
    			 
    	 System::TimeSpan diff = this->dtpWednesdayOut->Value.Subtract(this->dtpWednesdayIn->Value);
    			 
    	 int nbrHours;
    	 double nbrMinutes;
    	 String *strTotalTime = 0;
    
    	 int minutes = diff.Minutes + (diff.Hours * 60);
    	 nbrHours = minutes / 60;
    	 nbrMinutes = (minutes - (nbrHours * 60)) * 5 / 3;
    	 strTotalTime = String::Concat(nbrHours.ToString(), S".", nbrMinutes.ToString());
    
    	 this->txtWednesday->Text = strTotalTime;
    	CalculateWeeklyHours();
    }
    
    private: System::Void dtpThursdayIn_ValueChanged(System::Object *  sender, System::EventArgs *  e)
    {
    	 if( this->dtpThursdayIn->Value > this->dtpThursdayOut->Value )
    		 this->dtpThursdayIn->Value = this->dtpThursdayOut->Value;
    
    	 System::TimeSpan diff = this->dtpThursdayOut->Value.Subtract(this->dtpThursdayIn->Value);
    			 
    	 int nbrHours;
    	 double nbrMinutes;
    	 String *strTotalTime = 0;
    
    	 int minutes = diff.Minutes + (diff.Hours * 60);
    	 nbrHours = minutes / 60;
    	 nbrMinutes = (minutes - (nbrHours * 60)) * 5 / 3;
    	 strTotalTime = String::Concat(nbrHours.ToString(), S".", nbrMinutes.ToString());
    
    	 this->txtThursday->Text = strTotalTime;
    	CalculateWeeklyHours();
    }
    
    private: System::Void dtpThursdayOut_ValueChanged(System::Object *  sender, System::EventArgs *  e)
    {
    	 if( this->dtpThursdayOut->Value < this->dtpThursdayIn->Value )
    		 this->dtpThursdayOut->Value = this->dtpThursdayIn->Value;
    		 
    	 System::TimeSpan diff = this->dtpThursdayOut->Value.Subtract(this->dtpThursdayIn->Value);
    			 
    	 int nbrHours;
    	 double nbrMinutes;
    	 String *strTotalTime = 0;
    
    	 int minutes = diff.Minutes + (diff.Hours * 60);
    	 nbrHours = minutes / 60;
    	 nbrMinutes = (minutes - (nbrHours * 60)) * 5 / 3;
    	 strTotalTime = String::Concat(nbrHours.ToString(), S".", nbrMinutes.ToString());
    
    	 this->txtThursday->Text = strTotalTime;
    	CalculateWeeklyHours();
    }
    
    private: System::Void dtpFridayIn_ValueChanged(System::Object *  sender, System::EventArgs *  e)
    {
    	 if( this->dtpFridayIn->Value > this->dtpFridayOut->Value )
    		 this->dtpFridayIn->Value = this->dtpFridayOut->Value;
    
    	 System::TimeSpan diff = this->dtpFridayOut->Value.Subtract(this->dtpFridayIn->Value);
    			 
    	 int nbrHours;
    	 double nbrMinutes;
    	 String *strTotalTime = 0;
    
    	 int minutes  = diff.Minutes + (diff.Hours * 60);
    	 nbrHours     = minutes / 60;
    	 nbrMinutes   = (minutes - (nbrHours * 60)) * 5 / 3;
    	 strTotalTime = String::Concat(nbrHours.ToString(), S".", nbrMinutes.ToString());
    
    	 this->txtFriday->Text = strTotalTime;
    	CalculateWeeklyHours();
    }
    
    private: System::Void dtpFridayOut_ValueChanged(System::Object *  sender, System::EventArgs *  e)
    {
    	 if( this->dtpFridayOut->Value < this->dtpFridayIn->Value )
    		 this->dtpFridayOut->Value = this->dtpFridayIn->Value;
    			 
    	 System::TimeSpan diff = this->dtpFridayOut->Value.Subtract(this->dtpFridayIn->Value);
    			 
    	 int nbrHours;
    	 double nbrMinutes;
    	 String *strTotalTime = 0;
    
    	 int minutes  = diff.Minutes + (diff.Hours * 60);
    	 nbrHours     = minutes / 60;
    	 nbrMinutes   = (minutes - (nbrHours * 60)) * 5 / 3;
    	 strTotalTime = String::Concat(nbrHours.ToString(), S".", nbrMinutes.ToString());
    
    	 this->txtFriday->Text = strTotalTime;
    	CalculateWeeklyHours();
    }
    
    private: System::Void dtpSaturdayIn_ValueChanged(System::Object *  sender, System::EventArgs *  e)
    {
    	 if( this->dtpSaturdayIn->Value > this->dtpSaturdayOut->Value )
    		 this->dtpSaturdayIn->Value = this->dtpSaturdayOut->Value;
    
    	 System::TimeSpan diff = this->dtpSaturdayOut->Value.Subtract(this->dtpSaturdayIn->Value);
    			 
    	 int nbrHours;
    	 double nbrMinutes;
    	 String *strTotalTime = 0;
    
    	 int minutes = diff.Minutes + (diff.Hours * 60);
    	 nbrHours = minutes / 60;
    	 nbrMinutes = (minutes - (nbrHours * 60)) * 5 / 3;
    	 strTotalTime = String::Concat(nbrHours.ToString(), S".", nbrMinutes.ToString());
    
    	 this->txtSaturday->Text = strTotalTime;
    	CalculateWeeklyHours();
    }
    
    private: System::Void dtpSaturdayOut_ValueChanged(System::Object *  sender, System::EventArgs *  e)
    {
    	 if( this->dtpSaturdayOut->Value < this->dtpSaturdayIn->Value )
    		 this->dtpSaturdayOut->Value = this->dtpSaturdayIn->Value;
    			 
    	 System::TimeSpan diff = this->dtpSaturdayOut->Value.Subtract(this->dtpSaturdayIn->Value);
    			 
    	 int nbrHours;
    	 double nbrMinutes;
    	 String *strTotalTime = 0;
    
    	 int minutes = diff.Minutes + (diff.Hours * 60);
    	 nbrHours = minutes / 60;
    	 nbrMinutes = (minutes - (nbrHours * 60)) * 5 / 3;
    	 strTotalTime = String::Concat(nbrHours.ToString(), S".", nbrMinutes.ToString());
    
    	 this->txtSaturday->Text = strTotalTime;
    	CalculateWeeklyHours();
    }
    
    private: System::Void dtpSundayIn_ValueChanged(System::Object *  sender, System::EventArgs *  e)
    {
    	 if( this->dtpSundayIn->Value > this->dtpSundayOut->Value )
    		 this->dtpSundayIn->Value = this->dtpSundayOut->Value;
    
    	 System::TimeSpan diff = this->dtpSundayOut->Value.Subtract(this->dtpSundayIn->Value);
    			 
    	 int nbrHours;
    	 double nbrMinutes;
    	 String *strTotalTime = 0;
    
    	 int minutes = diff.Minutes + (diff.Hours * 60);
    	 nbrHours = minutes / 60;
    	 nbrMinutes = (minutes - (nbrHours * 60)) * 5 / 3;
    	 strTotalTime = String::Concat(nbrHours.ToString(), S".", nbrMinutes.ToString());
    
    	 this->txtSunday->Text = strTotalTime;
    	CalculateWeeklyHours();
    }
    
    private: System::Void dtpSundayOut_ValueChanged(System::Object *  sender, System::EventArgs *  e)
    {
    	 if( this->dtpSundayOut->Value < this->dtpSundayIn->Value )
    		 this->dtpSundayOut->Value = this->dtpSundayIn->Value;
    			 
    	 System::TimeSpan diff = this->dtpSundayOut->Value.Subtract(this->dtpSundayIn->Value);
    			 
    	 int nbrHours;
    	 double nbrMinutes;
    	 String *strTotalTime = 0;
    
    	 int minutes  = diff.Minutes + (diff.Hours * 60);
    	 nbrHours     = minutes / 60;
    	 nbrMinutes   = (minutes - (nbrHours * 60)) * 5 / 3;
    	 strTotalTime = String::Concat(nbrHours.ToString(), S".", nbrMinutes.ToString());
    
    	 this->txtSunday->Text = strTotalTime;
    	CalculateWeeklyHours();
    }
  12. Execute the application to test it
 

Home Copyright © 2004-2010 FunctionX, Inc.