[英]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文件,然后在設計器中打開了Form並單擊undo或類似的東西。
通過在設計器中打開表單,雙擊按鈕,然后將代碼移至設計器創建的新方法,您應該能夠使它們再次工作。
連接后,Form1.Designer.cs應包含以下行:
this.btnCalculate.Click += new System.EventHandler(this.btnCalculate_Click);
和清除按鈕類似。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.