2012-12-04 39 views
0
public partial class mainForm : Form 
{ 
    public mainForm() 
    { 
     InitializeComponent(); 
    } 
    private void btnCalculate_Click(object sender, EventArgs e) 
    { 
     string customerType = txtCustomerType.Text; 
     decimal subtotal = Convert.ToDecimal(txtSubtotal.Text); 
     decimal discountPercent = .0m; 

     switch (customerType) 
     { 
      case "R": 
       if (subtotal < 100) 
        discountPercent = .0m; 
       else if (subtotal >= 100 && subtotal < 250) 
        discountPercent = .1m; 
       else if (subtotal >= 250 && subtotal < 500) 
        discountPercent = .25m; 
       else if (subtotal >= 500) 
        discountPercent = .30m; 
       break; 
      case "C": 
       discountPercent = .2m; 
       break; 
      case "T": 
       if (subtotal < 500) 
        discountPercent = .4m; 
       else if (subtotal >= 500) 
        discountPercent = .5m; 
       break; 
      default: 
       discountPercent = .1m; 
       break; 
     } 

      decimal discountAmount = subtotal * discountPercent; 
      decimal invoiceTotal = subtotal - discountAmount; 

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

      txtCustomerType.Focus();    
     } 


    private void btnExit_Click(object sender, EventArgs e) 
    { 

     int i = 0; 
     string summaryString = txtTotal.Text; 
     for (i = 0; i < 5; i++) 
      summaryString += Environment.NewLine; 

     MessageBox.Show(summaryString, "Order Totals"); 

     this.Close(); 
    } 
} 

一旦用戶點擊計算按鈕,它就會計算髮票總額。我需要做的就是存儲前5個總值,並將它們一個接一個地顯示在一個消息框中。用戶單擊退出按鈕後,此消息框應顯示。顯示以前的值

+0

你應該把這個標記爲家庭作業,並告訴我們什麼是具體給你的問題。我們不是「複製粘貼使這項工作」社區。 –

+0

@SimonWhitehead家庭作業標籤已被棄用。 –

回答

0

像這樣的東西可能你指出正確的方向:

public partial class mainForm : Form 
{ 
    public mainForm() 
    { 
     InitializeComponent(); 
    } 

    private List<string> _totals = new List<string>(); 

    private void btnCalculate_Click(object sender, EventArgs e) 
    { 
      string customerType = txtCustomerType.Text; 
      decimal subtotal = Convert.ToDecimal(txtSubtotal.Text); 
      decimal discountPercent = .0m; 

      switch (customerType) 
      { 
       case "R": 
        if (subtotal < 100) 
         discountPercent = .0m; 
        else if (subtotal >= 100 && subtotal < 250) 
         discountPercent = .1m; 
        else if (subtotal >= 250 && subtotal < 500) 
         discountPercent = .25m; 
        else if (subtotal >= 500) 
         discountPercent = .30m; 
        break; 
       case "C": 
        discountPercent = .2m; 
        break; 
       case "T": 
        if (subtotal < 500) 
         discountPercent = .4m; 
        else if (subtotal >= 500) 
         discountPercent = .5m; 
        break; 
       default: 
        discountPercent = .1m; 
        break; 
      } 

      decimal discountAmount = subtotal * discountPercent; 
      decimal invoiceTotal = subtotal - discountAmount; 

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

      txtCustomerType.Focus(); 
     } 


     private void btnExit_Click(object sender, EventArgs e) 
     { 
      MessageBox.Show(string.Join(Environment.NewLine, _totals.ToArray()), "Order Totals"); 
      this.Close(); 
     } 

} 
0

試試這個。這會給你最後五個總數:

public partial class mainForm : Form 
{ 
public mainForm() 
{ 
    InitializeComponent(); 
} 

private List<string> allTotals = new List<string>(); 

private void btnCalculate_Click(object sender, EventArgs e) 
{ 
     string customerType = txtCustomerType.Text; 
     decimal subtotal = Convert.ToDecimal(txtSubtotal.Text); 
     decimal discountPercent = .0m; 

     switch (customerType) 
     { 
      case "R": 
       if (subtotal < 100) 
        discountPercent = .0m; 
       else if (subtotal >= 100 && subtotal < 250) 
        discountPercent = .1m; 
       else if (subtotal >= 250 && subtotal < 500) 
        discountPercent = .25m; 
       else if (subtotal >= 500) 
        discountPercent = .30m; 
       break; 
      case "C": 
       discountPercent = .2m; 
       break; 
      case "T": 
       if (subtotal < 500) 
        discountPercent = .4m; 
       else if (subtotal >= 500) 
        discountPercent = .5m; 
       break; 
      default: 
       discountPercent = .1m; 
       break; 
     } 

     decimal discountAmount = subtotal * discountPercent; 
     decimal invoiceTotal = subtotal - discountAmount; 

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

    if (allTotals.Count > 5) allTotals.RemoveAt(0); 

     txtCustomerType.Focus(); 
    } 


    private void btnExit_Click(object sender, EventArgs e) 
    { 
     MessageBox.Show(string.Join(Environment.NewLine, allTotals.ToArray()), "Order Totals"); 
     this.Close(); 
    } 

}