【问题标题】:C# Windows form How can I add previously entered intput to new input?C# Windows 窗体如何将以前输入的输入添加到新输入?
【发布时间】:2017-09-25 09:55:16
【问题描述】:

我正在构建一个表单,允许用户输入员工的姓名、工资率和支付期间的工作时间。将该信息输入三个文本框后,用户单击一个按钮,该按钮计算总工资、预扣社会保障、预扣医疗保险、预扣州税、预扣联邦税和净工资。此信息输出到标签。我希望用户能够输入多个员工,并将此信息添加到之前的输入中(如果第一个员工的总工资为 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);

        }
    }

【问题讨论】:

  • 我不太确定你的问题是什么。您是否要求类似: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);
  • 注意+=
  • 这会发生什么? 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);
  • 我的代码现在是这样,我计算了一名员工的总/净工资。我希望能够为多名员工执行此操作,并将他们的工资信息添加到不同的标签,以跟踪我输入的所有员工的总付款信息。
  • @glr22 如果您的要求真的那么简单,那么我上面的评论应该可以为您解决。

标签: c# winforms


【解决方案1】:

有很多方法,但我可能会制作一个控制器或类似的控制器,并让它存储一个员工类列表,其中员工类包含每个员工的所有信息。然后你可以通过简单地将列表中的总加起来从控制器(可能是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;
}

【讨论】:

  • 你的权利,这很丑陋....感谢你的帮助,但我似乎无法理解它。知道任何可能有助于学习做这类事情的资源吗?
  • 我不介意帮忙。这是一种常见的编程方法。只需考虑允许控制器管理数据(在这种情况下)而不是表单。
猜你喜欢
  • 1970-01-01
  • 2018-10-23
  • 1970-01-01
  • 2018-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多