Feed on
Posts
Comments

Last Update 9 September 2006.
What Time Do I Leave Friday is a program I created in C# using Visual C# 2005 Express Edition. It is the first "semi-practical" program I have made. (I say "semi-practical" as I don't have a way of using it at work, since I don't have a laptop.)

Simply stated, the program is designed for people who are mostly on Flex Time and want to find out when they should leave on their last day of the work week. The user enters the hours they worked so far, the time they came in that day, a lunch if they take one, and it will give them the time to leave.
You can get it from here.
I will post the code to it's own page to keep this page from getting too far out of hand.
Here are some screenshots:
The main screen:
Mian screen of my program, What Time Do I Leave Friday
Here the user enters the total hours they worked so far this week (using the action menu they can change the standard work week hours from 40:00), they also enter the time they got in Friday (or whatever their last day of work is) and a lunch if applicable. It will then display what time they are to leave Friday.

Here is the lunch screen:
Lunch screen of my program, What Time Do I Leave Friday
Here the user can choose to enter their lunch time (for example 0:30 for 30 minutes) or put in their time out to and in from lunch.

Here is the main screen after being used (I typically don't take a lunch, but for this example I entered one):
Main screen in use, after entering lunch and other information for my program, What Time Do I Leave Friday

Here is some of the code (I don't present the code for the designer since I figure it isn't necessary, it should be easy enough to figure it out from the screenshots, the program itself and the code below). The code is presented in alphabetical order.

Changes Log:
9 September 2006 - Minor changes to the Validator class. Semi-major change to the main window itself. The program now checks if the user entered a decimal time in one of the two main text boxes, if so, a message box comes up to be sure that is what they wanted to enter. I probably didn't handle that one the best way.

This is the Convert Time class that easily enough converts the time from one format to another for use in the program.

C#:
  1. // **********************************************
  2. // Program Name: What Time Do I Leave Friday
  3. // Program Author: Brian A. Thomas
  4. // Program Homepage: http://www.brianathomas.com/
  5. // Author Homepage: http://www.brianathomas.com/
  6. // **********************************************
  7. // This Namespace: WhatTimeFriday
  8. // NOTE: This will likely change in future versions
  9. // to be in my own library set.
  10. // **********************************************
  11. // Class: ConvertTime
  12. // Class Author: Brian A. Thomas
  13. // Class webpage: http://www.brianathomas.com/
  14. // This Class Version: 0.4
  15. // This Class Copyright: 2006 by Brian A. Thomas
  16. // **********************************************
  17. /*
  18.      This file is part of What Time Do I Leave Friday.
  19.     What Time Do I Leave Friday is free software; you can redistribute it
  20.     and/or modify it under the terms of the GNU General Public License as
  21.     published by the Free Software Foundation; version 2 of the
  22.     License, or any later version.
  23.     What Time Do I Leave Friday is distributed in the hope that it will be
  24.     useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  25.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  26.     GNU General Public License for more details.
  27.     You should have received a copy of the GNU General Public License
  28.     along with What Time Do I Leave Friday; if not, write to the Free Software
  29.     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  30. */
  31.  
  32. using System;
  33. using System.Collections.Generic;
  34. using System.Text;
  35. using System.Windows.Forms;
  36.  
  37. namespace WhatTimeFriday
  38. {
  39.     /// <summary>
  40.     /// ConvertTime Class has a few methods for converting time
  41.     /// currently it converts a standard format time such as 8:30 into
  42.     /// decimal time, and from military time to standard time
  43.     /// </summary>
  44.     class ConvertTime
  45.     {
  46.         /// <summary>
  47.         /// Changes time from standard format such as 8:30 into decimal time
  48.         /// (not the French version) but breaking minutes into hundreds. For
  49.         /// example 8:30 becomes 8.5.
  50.         /// </summary>
  51.         /// <param name="s">time in string format without the AM/PM</param>
  52.         /// <returns>time in double format</returns>
  53.         public static double ToDecimal(string s)
  54.         {
  55.             // had to add as canceling a lunch addition caused an error.
  56.             if (s == null)
  57.             {
  58.                 s = "0.00";
  59.             }
  60.             if (s.IndexOf(':') <0)
  61.             {
  62.                 // for some reason using a conditional or in the above didn't
  63.                 // work. It was set as (s.IndexOf(':') <0 || s.IndexOf('.')
  64.                 // <0) but if a string did have a : in it, it still went
  65.                 // to CantConvert
  66.                 if (s.IndexOf('.')>= 0)
  67.                 {
  68.                     // don't worry about it.
  69.                 }
  70.                 else
  71.                 {
  72.                     s = CantConvert(s);
  73.                 }
  74.             }
  75.             if (s.IndexOf(':')> -1) // check to see if it has a : in the time
  76.             //if so we need to turn it into a decimal time for
  77.             {
  78.                 // first split hours and minutes apart
  79.                 string[] allTime = s.Split(':');
  80.                 string hoursString = allTime[0];
  81.                 string minutesString = allTime[1];
  82.                 if (minutesString == "")
  83.                 {
  84.                     minutesString = "00";
  85.                 }
  86.                 //string secondsString = allTime[2];
  87.                 // above not used currently
  88.                 double minutes = Convert.ToInt32(minutesString);
  89.                 minutes /= 60;
  90.                 if (minutes <1 && minutes> 0)
  91.                 {
  92.                     string timeString = minutes.ToString();
  93.                     string[] fullTime = timeString.Split('.');
  94.                     string wholeString = fullTime[0];
  95.                     string portionString = fullTime[1];
  96.                     minutesString = portionString;
  97.                 }
  98.                 else
  99.                 {
  100.                     minutesString = "00";
  101.                 }
  102.                 string returnTime = hoursString + "." + minutesString;
  103.                 double time = Convert.ToDouble(returnTime);
  104.                 return time;
  105.             } // end if :
  106.             else
  107.             {
  108.                 string returnTime = s;
  109.                 // in theory, shouldn't need, but testing showed otherwise...
  110.                 // actually, should be gone for sure now after adding the
  111.                 // if(s==null) above, but will leave to be safe.
  112.                 if (returnTime == "")
  113.                 {
  114.                     returnTime = "0.00";
  115.                 }
  116.                 double time = Convert.ToDouble(returnTime);
  117.                 return time;
  118.             } // end else already in decimal time
  119.         }// end ToDecimal method of ConvertTime class
  120.  
  121.         /// <summary>
  122.         /// Converts from military time to standard time in the event the user
  123.         /// entered military time (24 hour time compared to 12 hour time)
  124.         /// </summary>
  125.         /// <param name="s">time in string format without the AM/PM</param>
  126.         /// <returns>time in string format with :</returns>
  127.         public static string NonMillTime(string s)
  128.         {
  129.             if (s == null)
  130.             {
  131.                 s = "0:00";
  132.             }
  133.             if (s.IndexOf(':') <0)
  134.             {
  135.                 // for some reason using a conditional or in the above didn't
  136.                 // work. It was set as (s.IndexOf(':') <0 || s.IndexOf('.')
  137.                 // <0) but if a string did have a : in it, it still went
  138.                 // to CantConvert
  139.                 if (s.IndexOf('.')>= 0)
  140.                 {
  141.                     // don't worry about it.
  142.                 }
  143.                 else
  144.                 {
  145.                     s = CantConvert(s);
  146.                 }
  147.             }
  148.             string[] allTime = s.Split(':');
  149.             string hoursString = allTime[0];
  150.             string minutesString = allTime[1];
  151.             if (minutesString == "")
  152.             {
  153.                 minutesString = "00";
  154.             }
  155.             // string secondsString = allTime[2];
  156.             // above not used currently
  157.             double hours = Convert.ToDouble(hoursString);
  158.             if (hours> 12)
  159.             {
  160.                 hours -= 12;
  161.                 WhatTimeFriday.MainWindow.PM = true;
  162.             } // end hours> 12
  163.             string returnTime = hours.ToString() + ":" + minutesString;
  164.             return returnTime;
  165.         } // end StandardTime Method of ConvertTime Class
  166.  
  167.         /// <summary>
  168.         /// Truns decimal time (not French version) to stnadard time with
  169.         /// a : as the time seperator. So 8.5 becomes 8:30.
  170.         /// </summary>
  171.         /// <param name="s">time in string format without the AM/PM</param>
  172.         /// <returns>time in string format with :</returns>
  173.         public static string StandardTime(string s)
  174.         {
  175.             // had to add as canceling adding a lunch caused an error
  176.             if (s == null)
  177.             {
  178.                 s = "0:00";
  179.             }
  180.             if (s.IndexOf(':') <0)
  181.             {
  182.                 // for some reason using a conditional or in the above didn't
  183.                 // work. It was set as (s.IndexOf(':') <0 || s.IndexOf('.')
  184.                 // <0) but if a string did have a : in it, it still went
  185.                 // to CantConvert
  186.                 if (s.IndexOf('.')>= 0)
  187.                 {
  188.                     // don't worry about it.
  189.                 }
  190.                 else
  191.                 {
  192.                     s = CantConvert(s);
  193.                 }
  194.             }
  195.             if (s.IndexOf(':')> -1)
  196.             {
  197.                 //already has a : so it is okay
  198.                 // had to add the split just in case user entered
  199.                 // :30 for lunch or something like that where the hour
  200.                 // string is empty
  201.                 string[] allTime = s.Split(':');
  202.                 string hoursString = allTime[0];
  203.                 string minutesString = allTime[1];
  204.                 if (minutesString == "")
  205.                 {
  206.                     minutesString = "00";
  207.                 }
  208.                 if (hoursString == "")
  209.                 {
  210.                     hoursString = "0";
  211.                 }
  212.                 s = hoursString + ":" + minutesString;
  213.                 return s;
  214.             } // end if s has a :
  215.             else
  216.             {
  217.                 if (s == "" || s == "0")
  218.                 {
  219.                     s = "0.00";
  220.                 }
  221.                 string[] allTime = s.Split('.');
  222.                 string hoursString = allTime[0];
  223.                 // just in case the hoursString is empty
  224.                 if (hoursString == "")
  225.                 {
  226.                     hoursString = "0";
  227.                 }
  228.                 string minutesString = allTime[1];
  229.                 if (minutesString == "")
  230.                 {
  231.                     minutesString = "00";
  232.                 }
  233.                 // string secondsString = allTime[2];
  234.                 // above not used currently
  235.                 minutesString = "." + minutesString;
  236.                 double minutes = Convert.ToDouble(minutesString);
  237.                 minutes *= 60;
  238.                 minutesString = minutes.ToString("f0"); //needs to remove the
  239.                 // decimal place if there is one, otherwise there are problems
  240.                 // probably will add to the seconds if that seconds are ever
  241.                 // calculated
  242.                 if (minutesString == "0")
  243.                 {
  244.                     minutesString = "00";
  245.                 }
  246.                 string returnTime = hoursString + ":" + minutesString;
  247.                 return returnTime;
  248.             }
  249.         } // end StandardTime method of ConvertTimeClass
  250.  
  251.         /// <summary>
  252.         /// Shouldn't actually be used as the method calling the ConvertTime
  253.         /// class should validate before sending.
  254.         /// If string coming into ConvertTiem doesn't have a : or a . in it
  255.         /// adds a :
  256.         /// </summary>
  257.         /// <param name="s">and integer in striang format</param>
  258.         /// <returns>a string with a : in it</returns>
  259.         private static string CantConvert(string s)
  260.         {
  261.             // Method calling the Convert Class should validate this before it
  262.             // sends it here, but just in case.
  263.             // presently I know of no way of knowing for sure which text box
  264.             // caused this from this class, which is why it should be validated
  265.             // by the method, class or something calling this class.
  266.             // Also, the string sent to the ConvertTime class may not be from a
  267.             // text box anyhow, so even knowing the text box may not help.
  268.             string sugestedTime;
  269.             if (s == "")
  270.             {
  271.                 s = "0";
  272.             }
  273.             int number = Convert.ToInt32(s);
  274.             if (number <0)
  275.             {
  276.                 // if the number is negative, reverse it
  277.                 number = -number;
  278.             }
  279.             if (number> 24 && number <60)
  280.             {
  281.                 // we'll guess that the user may want it in the minutes
  282.                 // spot if it is over 24 and under 60 since it can't be
  283.                 // more then 60 if it is minutes, and possibly minutes if
  284.                 // over 24 which would be the limit of the hours spot if it
  285.                 // is military time
  286.                 sugestedTime = "0:" + s;
  287.             }
  288.             else
  289.             {
  290.                 // this takes a huge range of 0 to 24 and from 60 on
  291.                 // as noted above, the calling method to the ConvertTime
  292.                 // class really needs to validate the string before sending
  293.                 // it here
  294.                 sugestedTime = s = ":00";
  295.             }
  296.             string message = "Can't convert " + s + " to a time.\n"
  297.             + "It needs a \":\" either before it (hours) or after it"
  298.             + "(minutes).\n"
  299.             + "Pressing OK will return " + sugestedTime + ". If this is not "
  300.             + "correct you will need to edit the entry when you return.";
  301.             MessageBox.Show(message, "Entry Error");
  302.             return sugestedTime;
  303.         } // end CantConvert method of ConvertTime class
  304.     } // end ConvertTime class
  305. }

Here is the code for the Hours Worked Calculator:

C#:
  1. // **********************************************
  2. // Program Name: What Time Do I Leave Friday
  3. // Program Author: Brian A. Thomas
  4. // Program Homepage: http://www.brianathomas.com/
  5. // Author Homepage: http://www.brianathomas.com/
  6. // **********************************************
  7. // This Namespace: WhatTimeFriday
  8. // **********************************************
  9. // This Class: HoursWorkedCalculator
  10. // This Class Author: Brian A. Thomas
  11. // This Class Version: 0.4
  12. // This Class Copyright: 2006 by Brian A. Thomas
  13. // **********************************************
  14. /*
  15.      This file is part of What Time Do I Leave Friday.
  16.     What Time Do I Leave Friday is free software; you can redistribute it
  17.     and/or modify it under the terms of the GNU General Public License as
  18.     published by the Free Software Foundation; version 2 of the
  19.     License, or any later version.
  20.     What Time Do I Leave Friday is distributed in the hope that it will be
  21.     useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  22.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23.     GNU General Public License for more details.
  24.     You should have received a copy of the GNU General Public License
  25.     along with What Time Do I Leave Friday; if not, write to the Free Software
  26.     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  27. */
  28.  
  29. using System;
  30. using System.Collections.Generic;
  31. using System.ComponentModel;
  32. using System.Data;
  33. using System.Drawing;
  34. using System.Text;
  35. using System.Windows.Forms;
  36.  
  37. namespace WhatTimeFriday
  38. {
  39.     /// <summary>
  40.     /// form for the Hours Worked Calculator
  41.     /// </summary>
  42.     public partial class HoursWorkedCalculator : Form
  43.     {
  44.         /// <summary>
  45.         /// Main class for the Hours Worked Calculator
  46.         /// </summary>
  47.         public HoursWorkedCalculator()
  48.         {
  49.             InitializeComponent();
  50.         }
  51.  
  52.         // global variable to track days added, user can only add up to 6 days
  53.         // since even if they were working 7 days, the main form keeps care
  54.         // of the 7th day
  55.         int dayAdded = 0;
  56.         string amPmOutBreak1 = "AM";
  57.         string amPmInBreak1 = "AM";
  58.         string amPmOutLunch = "AM";
  59.         string amPmInLunch = "AM";
  60.         string amPmOutBreak2 = "AM";
  61.         string amPmInBreak2 = "AM";
  62.         string lunchTime = "0:00";
  63.         private bool ok = true;
  64.         string amPmInForDay = "AM";
  65.         string amPMOutForDay = "PM";
  66.         string hoursWorkedWeek = "0:00";
  67.  
  68.         // user has the use clock radio button picked
  69.         private void rbnUseClock_CheckedChanged(object sender, EventArgs e)
  70.         {
  71.             grpHoursBox.Text = "Step 4";
  72.             grpSendBox.Text = "Step 5";