2017-04-18 36 views
-2

我是初學者這裏是我的代碼 我想從一個文本字段減去值到另一個,並顯示在第三個文本字段的結果,但問題是,我得到了錯誤輸入字符串格式沒有像 那樣格式正確,我該怎麼辦?從一個Textfield減去另一個有錯誤

我覺得我的代碼是正確的,但我不知道發生了什麼

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); 
    } 
} 
+2

你把數值在'pay_dues.Text'和'txt_pay2.Text'? –

+0

顯示你在txt文件夾中的內容。 – swdev95

+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); 
    } 
} 
+0

當我輸入數字值 它說,請只輸入數字消息 –

+0

'新正則表達式(@「\ D」)'匹配*負數*值像'-1' –

+0

@MuhammadOsama使用'(@「^ \ d $」) 「就像我更新。 –

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); 
     } 
0

私人無效textBox3_TextChanged(對象發件人,EventArgs的){ 嘗試 { 如果 (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); 
     } 
    } 
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; 
    } 
}