2016-10-13 6 views
0

数値が入力された後、いくつかの簡単な計算をtextboxes内で実行しています。コードは一桁の数字であれば正常に動作します。 しかし、数値が2桁(たとえば10以上)の場合は、計算を再実行できません。テキストボックスの値を継続的に更新するC#

これはTextChangedが使用されているためだとわかりませんが、助けがあれば助かります!

私のコードは次のとおりです。

private void textBox2_TextChanged(object sender, EventArgs e) 
{ 
    if (textBox2.Text.Length == 0) 
    { 

    } 
    else if (textBox4.Text.Length == 0) 
    { 
     percentage = Convert.ToDouble(textBox2.Text); 
     percentage = double.Parse(textBox2.Text); 
     percentage1 = percentage/100; 

     percentagecalc = percentage * total_loss; 

     rate = percentagecalc/0.5; 
     rateString = System.Convert.ToString(rate); 
     textBox4.Text = rateString; 

     volume = rate * 0.5; 
     volumeString = System.Convert.ToString(volume); 
     textBox5.Text = volumeString; 
    } 
    if (textBox2.Text.Length == 0) 
    { 
     textBox4.Text = string.Empty; 
     textBox5.Text = string.Empty; 
    }      
} 
+2

は、ブレークポイントを入れて、テキストボックス2と4は何 –

+0

をチェック!また、テキストボックス2が空であることを2回確認しているのはなぜですか? – ItamarG3

+1

debug、debug、debug – lordkain

答えて

2

Lengthによって確認していないが、TryParseによっては:

private void textBox2_TextChanged(object sender, EventArgs e) { 
    double p; 

    if (double.TryParse(textBox2.Text, out p)) { 
    // textBox2.Text has been changed and it contains double value - p 
    percentage = p; 

    percentage1 = percentage/100; 
    percentagecalc = percentage * total_loss; 
    rate = percentagecalc/0.5; 

    rateString = System.Convert.ToString(rate); 
    textBox4.Text = rateString; 

    volume = rate * 0.5; 
    volumeString = System.Convert.ToString(volume); 
    textBox5.Text = volumeString; 
    } 
    else { 
    // textBox2.Text has been changed, but it can't be treated as double 
    // (it's empty or has some weird value like "bla-bla-bla") 
    textBox4.Text = string.Empty; 
    textBox5.Text = string.Empty; 
    } 
} 
+0

これは美しく機能しました!どうもありがとう! – Loplac92

関連する問題