不断更新文本框c#中的值

Continually updating value in textbox c#

输入数字后,我正在 textboxes 中进行一些简单的计算。如果它们是个位数,则代码可以正常工作。 但是,如果数字是两位数(例如 10 及以上),它将无法再次 运行 计算。

我不确定这是否是因为使用了 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;
   }                       
}

不是通过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;
  }
}