2012-05-25 90 views
0

我已經通過這個網站找到了不同的幫助,但仍然無法將字符串轉換爲int。我嘗試了許多不同的方式。這裏有兩個。在button_click上,我需要讀取文本框並將它們轉換爲int,以便可以對它們執行標準邏輯。 (a> b功能)。第一部分下面是我用來在輸入文本框時強制使用數字。textbox接受整數

private void write_button_Click(object sender, EventArgs e) 
     { 
     int mat_od1 = int.Parse(matod_box.Text); //Input string in wrong format. 
     int mat_id1 = int.Parse(matid_box.Text); 
     int fod1 = int.Parse(fod_box.Text); 
     int fid1 = int.Parse(fid_box.Text); 
     int hp1 = int.Parse(hp_box.Text); 

     //This next section is just to show something else I've tried. 

     decimal mat_od = Convert.ToDecimal(matod_box.Text); //Same error. 
     decimal mat_id = Convert.ToDecimal(matid_box.Text); 
     decimal fod = Convert.ToDecimal(fod_box.Text); 
     decimal fid = Convert.ToDecimal(fid_box.Text); 
     decimal hp = Convert.ToDecimal(hp_box.Text); 
     decimal pass_od = mat_od; 

    } 

     private void fod_box_TextChanged(object sender, EventArgs e) 
    { 
     try 
     { 
      int numinput = int.Parse(fod_box.Text); 
      if (numinput < 1 || numinput > 500) 
      { 
       MessageBox.Show("You must enter a number between 0 and 500."); 
      } 
     } 
     catch (FormatException) 
     { 

      MessageBox.Show("You need to enter a number."); 
      fod_box.Clear(); 

     } 

任何幫助,將不勝感激。

+0

嘗試在第一個** int.parse **語句中設置一個斷點。查看** matod_box.Text **的值是什麼。 – Mark

+1

如果你在幾個地方有這些數字文本框,你應該考慮做一個用戶控件,繼承文本框並添加驗證。 – Yatrix

+0

我確實有多個盒子,所以好點。 – marcmiller2007

回答

4

,而不是int.Parse()你應該使用int.TryParse(string,out int)
這種方式,您將能夠赤輸出,並據此決定是否該字符串被解析正確與否

int i;string s=""; 
if(int.TryParse(s,out i)) 
{ 
//use i 
} 
else 
{ 
//show error 
} 
2

的int.parse轉換應該工作,如此示例:

string s = "111"; 
    int i; 
    if (int.TryParse(s, out i)) 
    { 
    Console.Write(i); 
    } 
    else 
    { 
     Console.Write("conversion failed"); 
    } 

您確定實際上爲您的整數提供合法輸入嗎?無論如何,你應該像我在我的示例中那樣使用TryParse。 Theres不需要使用try..catch,你可以使用框架提供的布爾方法,這將得到相同的結果。

+1

添加到YavgenyP的觀點,try..catch比布爾比較慢得多。異常會導致處理流程全部出錯!如果可以的話,避免它們。 – Mark

+0

@Mark。說例外應該是例外情況會更準確,所以你不應該使用它們來進行非例外處理,例如文本框已經存在。 –

1

所有取決於你允許放在文本框中。

如果它可能不是一個字符串,它可以轉換爲整數,包括空白,然後像

int value; 
if (int.TryParse(SomeString, out value) 
{ 
    // it is an int 
} 
else 
{ 
    // it's not an int, so do nothing raise a message or some such. 
} 
0

除了在按鈕的Click事件處理程序,其他使用Int32.TryParse所指出的那樣,你需要小心你在TextBox Changed事件處理程序中所做的事情。你在這裏的代碼是有缺陷的:

private void fod_box_TextChanged(object sender, EventArgs e) 
{ 
    try 
    { 
     int numinput = int.Parse(fod_box.Text); 
     ... 
    } 
    catch (FormatException) 
    { 
     MessageBox.Show("You need to enter a number."); 
     fod_box.Clear(); 
    } 

調用foo_box.Clear()將清除文本框中的所有文本,調用框TextChanged處理程序被再次執行(除非該文本框已經是空的)。因此,如果輸入非數字值,則消息框將顯示兩次 - 第一次嘗試解析非數字值時,第二次嘗試解析空字符串作爲調用的結果明確()。

通常,我會避免在Changed事件處理程序中進行驗證。

+0

EventArgs對象上沒有Handler屬性,如果它是非數字,他可以使用它嗎? – Yatrix

+0

感謝您的幫助! tryparse正在工作,喬你是正確的。該框確實顯示兩次。是否由於textchanged事件而具有清除功能。 – marcmiller2007

+0

「這個盒子會顯示兩次,不管是否由於textchanged而有明確的功能」 - 你可以通過明確地允許一個空字符串來解決這個問題。 – Joe