计算或清除不会工作

时间:2017-02-23 19:50:48

标签: c# button calculated-columns

我正在使用Windows窗体。每次按计算或清除时,都没有任何反应。表单加载但按钮不起作用。文本框保持清晰,没有值。 Visual Studio无法将代码识别为错误。有什么帮助吗?

    namespace WindowsFormsApplication8
    {
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }
        int numberOfInvoices = 0;
        decimal totalOfInvoices = 0m;
        decimal invoiceAverage = 0m;

    private void btnCalculate_Click(object sender, EventArgs e)
    {
        decimal txtSubtotal = Convert.ToDecimal(txtEnterSubtotal.Text);
        decimal discountPercent = .25m;
        if (txtSubtotal >= 500)
        {
            discountPercent = .2m;
        }
        else if (txtSubtotal >= 250 && txtSubtotal < 500)
        {
            discountPercent = .15m;
        }
        else if (txtSubtotal >= 100 && txtSubtotal < 250)
        {
            discountPercent = .1m;
        }
        else
        {
            discountPercent = .0m;
        }

        decimal discountAmount = Math.Round(txtSubtotal * discountPercent, 2);
        decimal invoiceTotal = txtSubtotal - discountAmount;

        this.txtSubtotal.Text = txtSubtotal.ToString("c");
        txtDiscountPercent.Text = discountPercent.ToString("p1");
        txtDiscountAmount.Text = discountAmount.ToString("c");
        txtTotal.Text = invoiceTotal.ToString("c");

        numberOfInvoices++;
        totalOfInvoices += invoiceTotal;
        invoiceAverage = totalOfInvoices / numberOfInvoices;

        txtNumberOfInvoices.Text = numberOfInvoices.ToString();
        txtTotalOfInvoices.Text = totalOfInvoices.ToString("c");
        txtInvoiceAverage.Text = invoiceAverage.ToString("c");

        txtEnterSubtotal.Text = "";
        txtEnterSubtotal.Focus();
    }

    private void btnClearTotals_Click(object sender, System.EventArgs e)
    {
        numberOfInvoices = 0;
        totalOfInvoices = 0m;
        invoiceAverage = 0m;

        txtNumberOfInvoices.Text = "";
        txtTotalOfInvoices.Text = "";
        txtInvoiceAverage.Text = "";

        txtEnterSubtotal.Focus();
    }
}
}

我非常感谢你的帮助,请让我知道如何改进。

1 个答案:

答案 0 :(得分:1)

您的点击处理程序似乎都运行正常。我猜你没有因为某种原因将它们连接到按钮对象。也许你创建了它们,保存了Form1.cs文件,然后在设计器中打开了Form并点击了undo或类似的东西。

您应该可以通过在设计器中打开表单,双击按钮并将代码移动到设计器创建的新方法,使它们再次工作。

连接后,Form1.Designer.cs应包含以下行:

this.btnCalculate.Click += new System.EventHandler(this.btnCalculate_Click);

和清除按钮类似。

相关问题