从一个Textfield减去另一个Textfield时出现错误

时间:2017-04-18 06:40:07

标签: c#

我是初学者,这是我的代码 我想从一个文本字段中减去值到另一个文本字段并在第三个文本字段中显示结果,但问题是我得到错误输入字符串格式不正确格式 那我该怎么办?

我认为我的代码是正确的,但我不知道发生了什么

private void txt_pay2_TextChanged(object sender, EventArgs e)
{
    try
    {
        if (!string.IsNullOrEmpty(txt_pay2.Text))
        {
           txt_dues2.Text = (Convert.ToInt32(pay_dues.Text) - Convert.ToInt32(txt_pay2.Text)).ToString();

        }
        else if (String.IsNullOrEmpty(txt_pay2.Text))
        {
            MessageBox.Show("Enter A Amount Please !!");
        }
    }
    catch (Exception Ex)
    {

        MessageBox.Show(Ex.Message);
    }
}

4 个答案:

答案 0 :(得分:0)

您需要验证文本框是否具有数值,并在有空格时修剪它们的值。

以下是如何做到的建议:

private void txt_pay2_TextChanged(object sender, EventArgs e)
{
    string text1 = pay_dues.Text.Trim();
    string text2 = txt_pay2.Text.Trim();

    try
    {
        if (!string.IsNullOrEmpty(text1 ))
        {
            // Validate both textboxes have numeric values:
            Regex numericRegex = new Regex(@"^-?\d*(\.\d+)?$");
            if ( (numericRegex.IsMatch(text1)) &&
                 (numericRegex.IsMatch(text2)) )
            {
                txt_dues2.Text = (Convert.ToInt32(text1) - Convert.ToInt32(text2)).ToString();
            }
            else
            {
                MessageBox.Show("Please enter only numbers !");
                return;
            }

        }
        else if (String.IsNullOrEmpty(txt_pay2.Text))
        {
            MessageBox.Show("Please enter an amount.");
        }
    }
    catch (Exception Ex)
    {

        MessageBox.Show(Ex.Message);
    }
}

答案 1 :(得分:0)

try
{  
  int n;
    bool isNumeric = int.TryParse(txt_pay2.Text, out n);
    if(isNumeric)
    {
     if (!string.IsNullOrEmpty(txt_pay2.Text))
                {
                   txt_dues2.Text = (Convert.ToInt32(pay_dues.Text) - Convert.ToInt32(txt_pay2.Text)).ToString();

                }
                else if (String.IsNullOrEmpty(txt_pay2.Text))
                {
                    MessageBox.Show("Enter A Amount Please !!");
                }
    }
    else
    { 
    MessageBox.Show("Enter A  Please !!");
    }
}
 catch (Exception Ex)
        {

            MessageBox.Show(Ex.Message);
        }

答案 2 :(得分:0)

private void textBox3_TextChanged(object sender,EventArgs e)         {             尝试             {                 if(!string.IsNullOrEmpty(textBox2.Text))                 {                     int val1 = Convert.ToInt32(textBox1.Text);                     int val2 = Convert.ToInt32(textBox2.Text);                     int result = val1 - val2;                     txtResult.Text = result.ToString();

            }
            else if (String.IsNullOrEmpty(textBox2.Text))
            {
                MessageBox.Show("Enter A Amount Please !!");
            }
        }
        catch (Exception Ex)
        {

            MessageBox.Show(Ex.Message);
        }
    }

答案 3 :(得分:0)

我建议这样的事情:

private void txt_pay2_TextChanged(object sender, EventArgs e) {
  // Contract:
  if ((String.IsNullOrEmpty(txt_pay2.Text)) {
    MessageBox.Show("Enter A Amount Please !!");

    // My suggestion (Win Forms): when asking user to enter anything (A Amount)
    // set keyboard focus for he/she start entering the value 
    if (txt_pay2.CanFocus)
      txt_pay2.Focus();

    return;
  } 

  // Try to get values:
  int Pay_Dues;
  int Pay;

  if (!int.TryParse(pay_dues.Text, out Pay_Dues))
    return; // Bad pay_dues.Text format
  else if (!int.TryParse(txt_pay2.Text, out Pay))
    return; // Bad txt_pay2.Text format 

  // We've met contract's requirements and we've got Pay_Dues, Pay values 

  // We don't want a cascade of txt_pay2.TextChanged events:
  // txt_pay2.Text = ... triggers txt_pay2_TextChanged 
  // which in turn calls txt_pay2.Text =  ...
  txt_pay2.TextChanged -= txt_pay2_TextChanged;

  try {
    txt_pay2.Text = (Pay_Dues - Pay).ToString(); 
  }
  finally {
    txt_pay2.TextChanged += txt_pay2_TextChanged;
  }
}