2013-08-21 40 views
2

我做了一個簡單的應用程序來將2個數字加在一起,但是當我將兩個字母加在一起或一個無效的符號使程序崩潰。如何創建一個消息框顯示的東西說:「請放置於數」當有人插入一個字母如何防止從C#中的無效輸入崩潰

這裏是我的代碼:

public partial class frmAdd : Form 
{ 
    string first; 
    string second; 
    public frmAdd() 
    { 
     InitializeComponent(); 
    } 

    private void btnFirst_Click(object sender, EventArgs e) 
    { 
     first = txtNumber.Text; 
    } 

    private void btnSecond_Click(object sender, EventArgs e) 
    { 
     second = txtNumber.Text; 
    } 

    private void btnResult_Click(object sender, EventArgs e) 
    { 
     int a = Convert.ToInt32(first); 
     int b = Convert.ToInt32(second); 
     int c = a + b; 
     txtResult.Text = c.ToString(); 
    } 
} 
+0

試試這個代碼
http://stackoverflow.com/questions/5028673/c-sharp-numeric-only-textbox-control – SK2185

回答

2

使用TryParse代替:

private void btnResult_Click(object sender, EventArgs e) 
{ 
    int a, b; 
    if (int.TryParse(first, out a) && int.TryParse(second, out b)) 
    { 
     int c = a + b; 
     txtResult.Text = c.ToString(); 
    } 
    else 
    { 
     MessageBox.Show("Invalid Input!"); 
    } 
} 

或者,也許更好的方法是在用戶首次輸入數據時捕獲錯誤:

public partial class frmAdd : Form 
{ 
    int first; // changed to int 
    int second; 

    private void btnFirst_Click(object sender, EventArgs e) 
    { 
     if (!int.TryParse(txtNumber.Text, out this.first)) 
     { 
      MessageBox.Show("Invalid Input!"); 
     } 
    } 

    private void btnSecond_Click(object sender, EventArgs e) 
    { 
     if (!int.TryParse(txtNumber.Text, out this.second)) 
     { 
      MessageBox.Show("Invalid Input!"); 
     } 
    } 

    private void btnResult_Click(object sender, EventArgs e) 
    { 
     int c = first + second; 
     txtResult.Text = c.ToString(); 
    } 
} 
+0

有哪些&&? – user2705420

+1

@ user2705420這就是[條件AND運算符](http://msdn.microsoft.com/en-us/library/2a723cdk.aspx)。它會嘗試解析'第一個',如果成功,將嘗試解析'second'。如果兩者都成功,它將執行內容'if'-block;否則它將執行'else'塊。 –

2

您可以使用NumericUpDown控件而不是TextBox - 它不允許用戶輸入無效數據。

或者您可以在用戶輸入內容後將驗證值添加到TextBox值。將ErrorProvider添加到您的表單中。並訂閱Validating事件txtNumber文本框。當文本框失去焦點時,將發生此事件。如果輸入的文字不是一個整數,那麼錯誤將被顯示在靠近texbox,你的按鈕將不會被點擊:

private void txtNumber_Validating(object sender, CancelEventArgs e) 
{ 
    int value; 
    if (!Int32.TryParse(txtNumber.Text, out value)) 
    { 
     errorProvider1.SetError(txtNumber, "Value is not an integer"); 
     return; 
    } 

    errorProvider1.SetError(txtNumber, ""); 
    first = value; // it's better to save integer value than text 
} 

驗證的樣子:

enter image description here

+0

NumericUpDown做什麼? – user2705420

+1

@ user2705420,'NumericUpDown'只是另一種類型的輸入控件,就像TextBox一樣,但是這個控件只允許輸入數字(帶或不帶小數)。「上下」來自控制器內的上下按鈕。 – gunr2171

+0

謝謝,你可以看到即時通訊的相當低的水平,但.SetError創建一個消息框或將顯示在txtNumber – user2705420

0

你可以加一點驗證以檢查是否可以通過傳入的string值創建intint.TryParse是一種簡單的方法。

而對於MessageBox你可以只使用MessageBox CALSS

例子:

private void btnResult_Click(object sender, EventArgs e) 
{ 

    int a = 0; 
    int b = 0; 
    if (!int.TryParse(first, out a)) 
    { 
     MessageBox.Show("first is not a number"); 
     return; 
    } 
    if (!int.TryParse(second, out b)) 
    { 
     MessageBox.Show("second is not a number"); 
     return; 
    } 
    int c = a + b; 
    txtResult.Text = c.ToString(); 
} 
0

而不是使用Convert.ToInt32(串),你可能會實際使用Int32.tryParse(字符串,OUT INT ),如下所示:

int a, b; 

a = Int32.tryParse(first, out a); 
b = Int32.tryParse(second, out b); 

如果轉換失敗,則tryParse方法將導致爲零。如果成功,它會給你在字符串中找到的號碼。

希望它能幫助你,不得不在C#的前幾天也找到它。