C#Windows窗体如何将以前输入的intput添加到新输入?

时间:2017-09-25 01:43:48

标签: c# winforms

我正在构建一个表单,允许用户输入员工的姓名,工资率和工资时间。一旦将该信息输入三个文本框,用户就会点击一个计算总薪酬,保留社会保障,保留医疗保险,扣缴州税,扣缴联邦税和净工资的按钮。此信息输出到标签。我希望用户能够输入多个员工,并将此信息添加到以前的输入中(如果第一个员工的总薪水为$ 2,000.00,第二个员工的总薪酬为$ 1,000,总薪酬为$ 3,000,等等。)我怎么能完成这个?

当前代码:

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);

        }
    }

1 个答案:

答案 0 :(得分:1)

有很多方法,但我只想做一个控制器可能或喜欢,并让它存储一个Employee类列表,其中Employee类拥有每个员工的所有信息。然后,您可以通过简单地从列表中添加总计来找到Controller中所有员工的运行总数(可能是EmployeeController)。

快速而丑陋的示例(编辑Employee和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;
}