2012-02-29 6 views
0

私は2つのボタン、テキストボックス、リストボックス、およびコンボボックスを持つフォームを持っています。私が持っている問題は、私がボタンを合計しようとしているときです。私はこのリストからコスト値を取得する方法を理解することができないので、注文の総コストを合計することができます。提案していただきありがとうございます。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

関連する問題