コード例 #1
0
        private void BAddToPayroll_Click(object sender, EventArgs e)    //Fires upon clicking the "Add" button
        {
            try
            {
                float    test = 0.0f;
                Employee temp = new Employee();                 //making employee to add to payroll
                temp.FirstName               = TBEmployeeNameFirst.Text;
                temp.LastName                = TBEmployeeNameLast.Text;
                temp.PhoneNumber             = TBPhoneNumber.Text;
                temp.Address.StreetAddress   = TBAddress.Text;
                temp.Address.ApartmentNumber = TBApartmentNumber.Text;
                temp.Address.City            = TBCity.Text;
                temp.Address.State           = TBState.Text;
                temp.Address.ZipCode         = TBZipCode.Text;
                temp.Title = TBJobTitle.Text;
                if (float.TryParse(TBWage.Text, out test))      //sanity check for wage
                {
                    temp.Wage = float.Parse(TBWage.Text);
                }
                temp.IsOnPayroll = CBIsOnPayroll.Checked;
                if (float.TryParse(TBHoursWorked.Text, out test))       //sanity check for hours worked
                {
                    temp.HoursWorked = float.Parse(TBHoursWorked.Text);
                }
                payroll[employeeIndex] = temp;                 //adds employee to payroll

                PayrollList.Items.Add(payroll[employeeIndex]); //adds employee to visible payroll
                employeeIndex++;                               //increment index
            }
            catch (PhoneNumberException)                       //Exception handling!
            {
                MessageBox.Show("Phone numbers must be ten digits!");
            }
            catch (WageException)
            {
                MessageBox.Show("Can't have negative wages!");
            }
            catch (TimeWorkedException)
            {
                MessageBox.Show("Can't have negative hours worked!");
            }
            catch (Exception)
            {
                MessageBox.Show("Something is wrong.\nThat's all we know.");
            }
            TBEmployeeNameFirst.Clear();        //returning fields to their default state
            TBEmployeeNameLast.Clear();
            TBPhoneNumber.Clear();
            TBAddress.Clear();
            TBApartmentNumber.Clear();
            TBCity.Clear();
            TBState.Clear();
            TBZipCode.Clear();
            TBJobTitle.Clear();
            TBWage.Clear();
            CBIsOnPayroll.Checked = false;
            TBHoursWorked.Clear();
        }
コード例 #2
0
        private void BModifyDone_Click(object sender, EventArgs e)           //fires on clicking the "Done" button
        {
            int index = PayrollList.Items.IndexOf(PayrollList.SelectedItem); //gets the index of the selected employee in the visible payroll

            Employee emp = (Employee)PayrollList.SelectedItem;               //copies the selected employee from the visible payroll to the backend

            PayrollList.Items.Remove(PayrollList.SelectedItem);              //removes the selected visible employee

            try
            {                                                   //alters the employee's data in the backend payroll using same methods as creating
                float test = 0.0f;
                emp.FirstName               = TBEmployeeNameFirst.Text;
                emp.LastName                = TBEmployeeNameLast.Text;
                emp.PhoneNumber             = TBPhoneNumber.Text;
                emp.Address.StreetAddress   = TBAddress.Text;
                emp.Address.ApartmentNumber = TBApartmentNumber.Text;
                emp.Address.City            = TBCity.Text;
                emp.Address.State           = TBState.Text;
                emp.Address.ZipCode         = TBZipCode.Text;
                emp.Title = TBJobTitle.Text;
                if (float.TryParse(TBWage.Text, out test))
                {
                    emp.Wage = float.Parse(TBWage.Text);
                }
                emp.IsOnPayroll = CBIsOnPayroll.Checked;
                if (float.TryParse(TBHoursWorked.Text, out test))
                {
                    emp.HoursWorked = float.Parse(TBHoursWorked.Text);
                }

                BModifyDone.Visible = false;    //hides the "Done" button

                BAddToPayroll.Visible = true;   //shows the "Add" button

                BModifyEmployee.Visible = true; //shows the "Edit" button

                BPayEmployee.Visible = true;    //shows the "Pay" button
            }
            catch (PhoneNumberException)        //Exception handling!
            {
                MessageBox.Show("Phone numbers must be ten digits!");
            }
            catch (WageException)
            {
                MessageBox.Show("Can't have negative wages!");
            }
            catch (TimeWorkedException)
            {
                MessageBox.Show("Can't have negative hours worked!");
            }
            catch (Exception)
            {
                MessageBox.Show("Something is wrong.\nThat's all we know.");
            }

            PayrollList.Items.Insert(index, emp); //"refreshes" the visible employee with the correct info

            TBEmployeeNameFirst.Clear();          //returns fields to the default state
            TBEmployeeNameLast.Clear();
            TBPhoneNumber.Clear();
            TBAddress.Clear();
            TBApartmentNumber.Clear();
            TBCity.Clear();
            TBState.Clear();
            TBZipCode.Clear();
            TBJobTitle.Clear();
            TBWage.Clear();
            CBIsOnPayroll.Checked = false;
            TBHoursWorked.Clear();
        }