2017-09-25 69 views
0

我正在構建一個表單,允許用戶輸入員工的姓名,工資率和工作時間。一旦將這些信息輸入到三個文本框中,用戶點擊一個按鈕,該按鈕可計算總收入,社會保障預扣,扣留醫療保險,扣留州稅,扣留聯邦稅和淨收入。這個信息被輸出到一個標籤。我希望用戶能夠輸入多個員工,並將此信息添加到之前的輸入中(如果第一個員工的總支付爲$ 2,000.00,第二個員工的總支付爲$ 1,000,總支付總額爲$ 3,000等) )我該如何去完成這件事?C#Windows窗體如何將先前輸入的輸入添加到新輸入中?

當前代碼:

public Employees() 
    { 
     InitializeComponent(); 
    } 

    private void SubmitBtn_Click(object sender, EventArgs e) 
    { 
     String name; 
     decimal hours, payrate, grosspay; 
     Boolean error = false; 

     name = NameBox.Text; 
     if (NameBox.Text == string.Empty) 
     { 
      MessageBox.Show("You must enter a full name into the name textbox."); 
      error = true; 
      NameBox.Focus(); 
     } 
     hours = decimal.Parse(HoursWorkedBox.Text); 
     payrate = decimal.Parse(HourRateBox.Text); 
     if (hours < 0 || payrate < 0) 
     { 
      MessageBox.Show("Cannot have negative hours/hourly rate."); 
      error = true; 
      HoursWorkedBox.Focus(); 
     } 


     if (error == false) 
     { 
      decimal netpay, sswithheld, mcwithheld, statetaxwithheld, fedtaxwithheld; 
      grosspay = hours * payrate; 
      sswithheld = grosspay * .061m; 
      mcwithheld = grosspay * .0143m; 
      statetaxwithheld = 0; 
      fedtaxwithheld = 0; 
      if (grosspay > 0 && grosspay < 600) 
      { 
       statetaxwithheld = grosspay * .02m; 
       fedtaxwithheld = grosspay * .05m; 
      } 
      else if (grosspay >= 600 && grosspay < 1100) 
      { 
       statetaxwithheld = grosspay * .04m; 
       fedtaxwithheld = grosspay * .10m; 
      } 
      else if (grosspay >= 1100 && grosspay < 1600) 
      { 
       statetaxwithheld = grosspay * .06m; 
       fedtaxwithheld = grosspay * .15m; 
      } 
      else if (grosspay >= 1600 && grosspay < 2100) 
      { 
       statetaxwithheld = grosspay * .06m; 
       fedtaxwithheld = grosspay * .20m; 
      } 
      else if (grosspay >= 2100 && grosspay < 3100) 
      { 
       statetaxwithheld = grosspay * .06m; 
       fedtaxwithheld = grosspay * .25m; 
      } 
      else if (grosspay > 3100) 
      { 
       statetaxwithheld = grosspay * .06m; 
       fedtaxwithheld = grosspay * .30m; 
      } 
      netpay = grosspay - fedtaxwithheld - statetaxwithheld - sswithheld - mcwithheld; 
      OutputLbl.Text = ("Employee name: " + name + "\nGross pay: " + grosspay + "\nFederal Tax Withheld : " + fedtaxwithheld + "\nState Tax Withheld: " + statetaxwithheld + "\nSocial Security Withheld: " + sswithheld + "\nMedicare Withheld: " + mcwithheld + "\nNet Pay: " + netpay); 

     } 
    } 
+0

我不太清楚你的問題是。您是否要求如下所示:'OutputLbl.Text + =(「員工姓名:」+姓名+「\ nGross pay:」+ grosspay +「\ n聯邦稅代扣:」+ fedtaxwithheld +「\ n國家稅代扣:」+ statetaxhold +「\ nSocial Security版主:」+ sswithheld +「\ nMedicare版主:」+ mcwithheld +「\ nNet Pay:」+ netpay);' – mydogisbox

+0

注意'+ =' – mydogisbox

+0

這會發生什麼? 'OutputLbl.Text =(「員工姓名:」+姓氏+「\ n交叉薪水:」+ grosspay +「\ n聯邦稅保留:」+ fedtaxwithheld +「\ n國家稅代扣:」+ statetaxwithheld +「\ n社會安全代扣:」 + sswithheld +「\ nMedicare版主:」+ mcwithheld +「\ nNet Pay:」+ netpay); ' –

回答

1

的方法有很多,但就在我的頭頂,我會做一個控制器可能還是喜歡,並讓它商店僱員類的列表,其中Employee類有所有員工的所有信息。然後,您可以通過簡單地將列表中的總量加起來,從Controller中找到所有員工的總運行總數(EmployeeController也許)。

快速和醜陋的例子(編輯員工和EmployeeController滿足您的需求):

public class Employee 
{ 
    public string Name { get; set; } 
    public double Pay { get; set; } 
} 

public class EmployeeController 
{ 
    private static EmployeeController employeeController = new EmployeeController(); 
    private { Employees = new List<Employee>(); } 
    public static EmployeeController Instance => employeeController; 

    public List<Employee> Employees { get; set; } 

    public double TotalPay 
    { 
     get 
     { 
       var totalPay = 0d; 

       foreach (var employee in Employees) 
        totalPay += employee.Pay; 

       return totalPay; 
     } 
    } 
} 

然後你可以使用它像這樣...

public void SomeMethod() 
{ 
    var newEmployee = new Employee() 
    { 
      Name = textboxName.Text, 
      Pay = double.Parse(textboxPay.Text) 
    } 

    EmployeeController.Instance.Employees.Add(newEmployee); 

    labelEmployeePay.Text = newEmployee.Pay; 
    labelTotalPay.Text = EmployeeController.Instance.TotalPay; 
} 
+0

你的權利,這是非常醜陋....感謝你的幫助,但我似乎無法理解它。瞭解可能有助於學習如何做這類事情的任何資源? – glr22

+0

我不介意幫忙。這是編程的常用方法。考慮允許控制器管理數據(在這種情況下)而不是形式。 –