2012-02-29 2 views
0

두 개의 단추, 텍스트 상자, 목록 상자 및 콤보 상자가있는 폼이 있습니다. 내가 가지고있는 문제는 내가 총 버튼을하려고 할 때입니다. 이 목록에서 비용 값을 얻는 방법을 알 수 없으므로 주문의 총 비용을 합산 할 수 있습니다. 제안 해줘서 고마워.C# windows 폼 목록 상자 합계

private void Order_Click(object sender, EventArgs e) 
    { 
     amount = int.Parse(textBox1.Text); 
     cost *= amount; 

     if (comboBox1.SelectedItem.ToString() == "Eggs") 
     { 
      listBox1.Items.Add("The cost for " + amount + " 
      dozen large, fresh eggs is: " + (cost).ToString("C")); 
      comboBox1.Text = ""; 
      textBox1.Text = "0"; 
      textBox1.Focus(); 
      comboBox1.Focus(); 
     } 
     else if (comboBox1.SelectedItem.ToString() == "Milk") 
     { 
      if (amount == 1) 
      { 
       listBox1.Items.Add 
       ("The Cost for a fresh quart milk is: " + (cost).ToString("C")); 
       comboBox1.Text = ""; 
       comboBox1.Focus(); 
       textBox1.Text = "0"; 
       textBox1.Focus(); 
      } 
      else 
      { 
       listBox1.Items.Add 
      ("The Cost for " + amount + " quarts of fresh milk is: " + 
       (cost).ToString("C")); 
       comboBox1.Text = ""; 
       comboBox1.Focus(); 
       textBox1.Text = "0"; 
       textBox1.Focus(); 
      } 
     } 
     else 
     { 
      listBox1.Items.Add 
      ("The Cost for " + amount + " fresh loafs of bread is: " + (cost).ToString("C")); 
      comboBox1.Text = ""; 
      comboBox1.Focus(); 
      textBox1.Text = "0"; 
      textBox1.Focus(); 
     } 
    } 

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     if (comboBox1.SelectedItem.ToString() == "Eggs") 
     { 
      cost = 1.90; 
      label1.Text = "The Cost is " + (cost).ToString("C") + " per dozen"; 
     } 
     else if (comboBox1.SelectedItem.ToString() == "Milk") 
     { 
      cost = 1.47; 
      label1.Text = "The Cost is " + (cost).ToString("C") + " per quart"; 
     } 
     else 
     { 
      cost = 2.12; 
      label1.Text = "The Cost is " + (cost).ToString("C") + " per loaf"; 
     } 
    } 

    private void total_Click(object sender, EventArgs e) 
    { 


    } 

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e) 
    { 
     if (!char.IsControl(e.KeyChar) 
      && !char.IsDigit(e.KeyChar) 
      && e.KeyChar != '.') 
     { 
      e.Handled = true; 
     } 

     if (e.KeyChar == '.' 
    && (sender as TextBox).Text.IndexOf('.') > -2) 
     { 
      e.Handled = true; 
     } 
     if (e.KeyChar == '-' 
&& (sender as TextBox).Text.IndexOf('-') > -2) 
     { 
      e.Handled = true; 
     } 
    } 
} 

}

답변

1

왜 총 클래스 필드를 사용하고 여기에 목록 상자에 항목을 추가 할 때마다 금액을 추가하지? 그런 다음 목록에서 다시 구문 분석하지 않고도 항상 총 비용을 부담하게됩니다.

public partial class Form1 : Form 
{ 
    private double cost; 
    private double total; 
    int amount = 0; 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void Order_Click(object sender, EventArgs e) 
    { 
     amount = int.Parse(textBox1.Text); 
     cost *= amount; 
     total += cost; 
     ... 
    } 
+0

감사합니다. 사용해보세요. – Sloshy