//Convert starting date to weekspan (all strings) public static string DateToWeekSpan(string dateStr) { //Parse value DateTime dateVal = new DateTime(); if (UtilDotNET.ValidateDate(dateStr)) { dateVal = UtilDotNET.StringToDate(dateStr); } else { return("ERROR: Please fix starting date from database table"); } //Last Monday int deltaToLastMonday = DayOfWeek.Monday - dateVal.DayOfWeek; DateTime lastMonday = dateVal.AddDays(deltaToLastMonday).Date; //This sunday int deltaToThisSunday = DayOfWeek.Sunday - dateVal.DayOfWeek; if (deltaToThisSunday <= (int)dateVal.DayOfWeek) { deltaToThisSunday += 7; } DateTime thisSunday = dateVal.AddDays(deltaToThisSunday).Date; string weekspan = String.Format("{0}-{1}", lastMonday.ToString("MM/dd/yy"), thisSunday.ToString("MM/dd/yy")); return(weekspan); }
/* ================================================================================ * Event for combo box items change * - Includes display * - Changes current workweek * ================================================================================ */ private void cmbWeekSpan_SelectedValueChanged(object sender, EventArgs e) { //When initialized disable buttons save, back, and forth. if (cmbWeekSpan.Text == "") { btnSave.Enabled = false; btnBack.Enabled = false; btnForth.Enabled = false; } else { btnSave.Enabled = true; btnBack.Enabled = true; btnForth.Enabled = true; //Disable forth DateTime cmbDate = WorkWeek.LastBeginningOfWeek(currentWorkWeek.Week); //Date we are in DateTime cmbMonday = WorkWeek.LastBeginningOfWeek(cmbDate.ToString()); DateTime date = UtilDotNET.StringToDate(WorkWeek.ConvertWeekSpanToBeginningOfWeek(cmbWeekSpan.Text)); //Date we want to go to (because it change) //MessageBox.Show(cmbMonday + " " + date); //Only execute on different date selections if (cmbMonday != date) { //Load corresponding week int currentIndex = cmbWeekSpan.Items.IndexOf(cmbWeekSpan.Text); currentWorkWeek = employeeWorkWeekList[currentIndex]; Display(currentWorkWeek); } } }
//Updates Employee's information private void btnUpdate_Click(object sender, EventArgs e) { if (!UtilDotNET.IsMatch(UtilDotNET.phonePattern1, txtPhone.Text)) { MessageBox.Show("Error: not a valid phone format."); return; } if (!UtilDotNET.IsMatch(UtilDotNET.emailPattern, txtEmail.Text)) { MessageBox.Show("Error: invalid email format."); return; } if (!UtilDotNET.ValidateDate(txtStartingDate.Text) && txtStartingDate.Text != "") { MessageBox.Show("Invalid date format"); return; } EmployeeDatabase.UpdateEmployee(txtFirstName.Text, txtLastName.Text, txtDepartment.Text, txtPhone.Text, txtEmail.Text, txtStartingDate.Text); MessageBox.Show(string.Format("Updated: {0} {1}", txtFirstName.Text, txtLastName.Text)); //Update listview display this.Form2_Load(this, null); }
//Reset "hint" on leave if nothing was written private void cell_Leave(object sender, EventArgs e) { TextBox cell = (TextBox)sender; //Reset default value on leave, if nothing was written CheckForDummies(cell); //If military time, and in columns of military time, then proceed if (timeAreas.IndexOf(ParseCellName(cell.Name)[2]) != -1 && cell.Text != "") { if (!UtilDotNET.ValidateMilitaryTextFormat(cell.Text)) //Leaving empty cell { cell.Text = "0000"; cell.ForeColor = Color.DarkGray; } else { //Update grid cell with valid time input GridCellUpdate(cell); //Check valid time input: greater than previous //- Date format known to be valid (tryparse exact in validatemilitary...) DateTime dateLeaving = DateTime.ParseExact(cell.Text, "HHmm", CultureInfo.InvariantCulture, DateTimeStyles.None); Point pos = GridPos(cell); int movingLeft = pos.Y; while (movingLeft > 0) //Columns to the left { movingLeft--; //Starting for first column to the left of current cell string comparisonText = grid[pos.X][movingLeft]; //Text from left cell DateTime tempDate; if (DateTime.TryParseExact(comparisonText, "HHmm", CultureInfo.InvariantCulture, DateTimeStyles.None, out tempDate)) { //Pass - text is military time if (tempDate > dateLeaving) //Break; after check against a military time { cell.Text = "0000"; cell.ForeColor = Color.DarkGray; } break; } } } } }
/* ==================================================== * GetLastMondayFromDate * > Error-catch any possible undesirable change in table values * ==================================================== */ public static DateTime LastBeginningOfWeek(string dateStr) { DateTime date = UtilDotNET.StringToDate(dateStr); //Last Monday if (date != DateTime.MinValue) { int deltaToLastMonday = DayOfWeek.Monday - date.DayOfWeek; if (deltaToLastMonday > 0) { deltaToLastMonday -= 7; } DateTime lastMonday = date.AddDays(deltaToLastMonday).Date; return(lastMonday); } else { return(date); } }
//Clear cells on click private void cell_Click(object sender, EventArgs e) { TextBox cell = (TextBox)sender; if (allCells.Contains(cell)) { //If cell has dummy if (dummies.IndexOf(cell.Text) != -1) { cell.Text = ""; cell.ForeColor = SystemColors.WindowText; //WindowText is the default color for text boxes } if (UtilDotNET.ValidateMilitaryTextFormat(cell.Text)) { //Upgrade grid cell with new info GridCellUpdate(cell); //Display Hours double hrs = CalculateHours(); lblTotal.Text = Math.Round(hrs, 1).ToString(); } } }
//Submit button event, add employee, update auto complete, and include employee in timesheet private void btnSubmit_Click(object sender, EventArgs e) { //Exit this function if there is any blank text leave out if (txtFirstName.Text == "" || txtLastName.Text == "" || txtDepartment.Text == "" || txtPhone.Text == "" || txtEmail.Text == "") { MessageBox.Show("Missing data; please fill in all fields."); return; } /* ================================================================== * Format invariants * - Phone: xxx-xxx-xxxx * - Department: enum match * - Email: _@_.com * - Date: MM/dd/yyyy (just tryparse) */ if (!UtilDotNET.IsMatch(UtilDotNET.phonePattern1, txtPhone.Text)) { MessageBox.Show("Error: not a valid phone format."); return; } if (!UtilDotNET.IsMatch(UtilDotNET.emailPattern, txtEmail.Text)) { MessageBox.Show("Error: invalid email format."); return; } if (!UtilDotNET.ValidateDate(txtStartingDate.Text) && txtStartingDate.Text != "") { MessageBox.Show("Invalid date format"); return; } //Exit if employee is already in list Employee solicitude = EmployeeDatabase.GetEmployee(txtFirstName.Text, txtLastName.Text); if (solicitude == null) { MessageBox.Show(string.Format("Employee {0} {1} already in records.", solicitude.FirstName, solicitude.LastName)); return; } EmployeeDatabase.AddEmployee(txtFirstName.Text, txtLastName.Text, txtDepartment.Text, txtPhone.Text, txtEmail.Text, txtStartingDate.Text); //Load all employees allEmployees = EmployeeDatabase.GetAllEmployees(); UtilWinforms.UpdateAutoComplete(ref allTextBoxControls, ref allEmployees); //Update AutoComplete values //Some feedback on add employee MessageBox.Show("Added 1 employee to Employee table."); //Update timesheet table with new employee - name and employee id, the rest of the fields empty Employee employee = EmployeeDatabase.GetEmployee(txtFirstName.Text, txtLastName.Text); //Add to timsheet table TimeSheetDatabase.AddWeekWork(employee.EmployeeID.ToString(), employee.FirstName, employee.LastName, WorkWeek.zeroDummy, WorkWeek.zeroDummy, WorkWeek.zeroDummy, WorkWeek.zeroDummy, WorkWeek.ellipsisDummy, WorkWeek.dashDummy, WorkWeek.dashDummy, employee.StartingDate); //Clear out the form txtFirstName.Text = ""; txtLastName.Text = ""; txtDepartment.Text = ""; txtPhone.Text = ""; txtEmail.Text = ""; txtStartingDate.Text = ""; this.Form2_Load(this, null); //Update listview MessageBox.Show("Employee added to timesheet records.\nYou can start now updating employee's work days!"); }