计算或清除行不通
Calculate or Clear wont' work
我正在处理 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();
}
}
}
非常感谢您的帮助,请告诉我如何改进。
您的两个点击处理程序似乎都工作正常。我猜你出于某种原因没有将它们连接到按钮对象。也许你创建了它们,保存了 Form1.cs 文件,然后在设计器中打开了表单并点击了撤消或类似的东西。
您应该能够通过在设计器中打开表单、双击按钮并将代码移动到设计器创建的新方法来使它们再次工作。
连接后,Form1.Designer.cs 应包含以下行:
this.btnCalculate.Click += new System.EventHandler(this.btnCalculate_Click);
与清除按钮类似。
我正在处理 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();
}
}
}
非常感谢您的帮助,请告诉我如何改进。
您的两个点击处理程序似乎都工作正常。我猜你出于某种原因没有将它们连接到按钮对象。也许你创建了它们,保存了 Form1.cs 文件,然后在设计器中打开了表单并点击了撤消或类似的东西。
您应该能够通过在设计器中打开表单、双击按钮并将代码移动到设计器创建的新方法来使它们再次工作。
连接后,Form1.Designer.cs 应包含以下行:
this.btnCalculate.Click += new System.EventHandler(this.btnCalculate_Click);
与清除按钮类似。