2017-04-12 1 views
0

I次のコードを持っている:私のプログラムは数値から引き算を続けなければならないと教えてください。

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    double salaryAmount; 
    double subtractAmount; 
    double newSalaryAmount; 
    string amountBackToString; 

    private void Button1_Click(object sender, RoutedEventArgs e) 
    { 
     try 
     { 
      salaryAmount = double.Parse(TextBox1.Text); 
      label1.Content = salaryAmount.ToString(); 
     } 
     catch (Exception) 
     { 

      MessageBox.Show("This was not a valid input."); 
     } 
    } 

    private void Button2_Click(object sender, RoutedEventArgs e) 
    { 
     try 
     { 
      subtractAmount = double.Parse(TextBox2.Text); 
      newSalaryAmount = salaryAmount - subtractAmount; 
      label1.Content = newSalaryAmount.ToString(); 
     } 
     catch (Exception) 
     { 
      MessageBox.Show("This was not a valid input."); 
     } 
    } 

    private void Button3_Click(object sender, RoutedEventArgs e) 
    { 
     amountBackToString = Convert.ToString(newSalaryAmount); 
     string[] currentAmount = { amountBackToString }; 
     File.WriteAllLines(@"C:\Users\Silas\Downloads\Lohn.txt", currentAmount); 
    } 

    private void Button4_Click(object sender, RoutedEventArgs e) 
    { 
     string text = File.ReadAllText(@"C:\Users\Silas\Downloads\Lohn.txt"); 
     label1.Content = text; 
     salaryAmount = Convert.ToDouble(label1.Content); 
    } 
} 

問題は、それが数に初期入力するたびにリセットしてから減算しますが、それは初期入力を取り、それが

私のプログラムから引いておくべきであるということです給料計算機です。あなたは単にあなたが再び値を小さくするために行くだろうというときsalaryAmountはいつもTextBox1.Textから与えられた初期値とどまるあなたのケースでは、あなたの現在のsalaryAmount

private void Button2_Click(object sender, RoutedEventArgs e) 
{ 
    try 
    { 
     subtractAmount = double.Parse(TextBox2.Text); 
     newSalaryAmount = salaryAmount - subtractAmount; 

     //Set the new current salary 
     salaryAmount = newSalaryAmount 
     label1.Content = newSalaryAmount.ToString(); 
    } 
    catch (Exception) 
    { 
     MessageBox.Show("This was not a valid input."); 
    } 
} 

newSalaryAmountを適用する必要が

enter image description here

答えて

2

それ以来変更されていないその値をとるでしょう。

+0

ありがとうございました! – Paludis

+0

@パルディス歓迎です:) – CNuts

関連する問題