2013-11-25 47 views
1

我知道在這個問題上有很多線程,但我找不到有效的解決方案。我對c#很陌生,所以我不完全確定要做什麼。基本上這種方法是,當用戶在textWithdraw.text文本框中輸入一個值時,它們會發出輸入字母等錯誤信息,或者顯示一條消息顯示他們要撤回的金額。我仍在研究使其更高效的方法。 txtBalance.text是用戶現有的餘額,任何人想知道它的用途。 這是我得到錯誤的代碼位。輸入字符串格式不正確(c#簡單方法)

error= double numberEntered = double.Parse(txtWithdraw.Text); 
whole method 
private void newBalance() 
    { 
     double numberEntered = double.Parse(txtWithdraw.Text); 
     double balance = double.Parse(txtBalance.Text); 
     double newBalance = balance - numberEntered; 
     txtBalance.Text=((balance - numberEntered).ToString()); 

     if (numberEntered < 1 || numberEntered > 9999999) 
     { 
      MessageBox.Show("You must enter a number between 1 and 10"); 
     } 

     else 
     MessageBox.Show(String.Format(numberEntered.ToString()), "You have withdrawn"); 
     txtWithdraw.Text = ""; 
     // MessageBox.Show(newBalance.ToString(),numberEntered.ToString()); 
    } 
+4

無論txtWithdraw或txtBalance包含不能被解析到雙號 –

+3

注意'double.Parse'預計數爲默認情況下在用戶的文化文本。所以如果你在一個需要小數點逗號而不是點的國家,你需要遵循這一點(它不會模仿C#的'double'文字格式)。 – Joey

+0

您可能想要添加某種輸入驗證,try/catch或使用TryParse。 – crashmstr

回答

0

使用Double.TryParse方法檢查字符串是否可以解析爲雙倍。此方法返回布爾值而不是拋出異常。檢查該值,並通知用戶,如果格式錯誤數據:

private void newBalance() 
{  
    double withdraw; 
    if (!double.TryParse(txtWithdraw.Text, out withdraw)); 
    { 
     MessageBox.Show("Please, enter correct withdraw number"); 
     return; 
    } 

    if (withdraw < 1 || 9999999 < withdraw) 
    { 
     MessageBox.Show("You must enter a number between 1 and 10"); 
     return; 
    } 

    double balance; 
    if (!double.TryParse(txtBalance.Text, out balance)); 
    { 
     MessageBox.Show("Please, enter correct balance number"); 
     return; 
    } 

    balance -= withdraw; 
    txtBalance.Text = balance.ToString(); 
    MessageBox.Show(String.Format(withdraw.ToString()), "You have withdrawn"); 
    txtWithdraw.Text = ""; 
} 

我也建議你處理Validating事件文本框的檢查,如果有人進來後,只是在正確的格式值。請參閱User Input Validation in Windows Forms。或者使用NumericUpDown控制來避免非數字輸入。

0

您可以使用Double.TryParse()函數檢查給定的輸入是否可以轉換爲double。

從MSDN:Double.TryParse()

一個數字的字符串表示形式轉換指定樣式 和區域性特定格式到其雙精度浮點 數當量。返回值指示轉換 是成功還是失敗。

試試這個:

private void newBalance() 
{ 
    double numberEntered ; 
    double balance ; 
    if(double.TryParse(txtWithdraw.Text,System.Globalization.NumberStyles.None,System.Globalization.CultureInfo.InvariantCulture,out numberEntered)) 
    { 
    if(double.TryParse(txtBalance.Text,System.Globalization.NumberStyles.None,System.Globalization.CultureInfo.InvariantCulture,out balance)) 
    { 
     double newBalance = balance - numberEntered; 
     txtBalance.Text=((balance - numberEntered).ToString()); 

     if (numberEntered < 1 || numberEntered > 9999999) 
     { 
      MessageBox.Show("You must enter a number between 1 and 10"); 
     } 

     else 
     MessageBox.Show(String.Format(numberEntered.ToString()), "You have withdrawn"); 
     txtWithdraw.Text = ""; 
     // MessageBox.Show(newBalance.ToString(),numberEntered.ToString()); 
    } 
    } 
}