2012-01-26 86 views
-4

當我運行一些我放在'if'語句之間的代碼時,我的程序失敗了。 我知道爲什麼上一個任務太多的依賴,以檢測高於9工作 數< 10或> 9 數字,但有一個數字的值將導致異常在此位置:if和else循環在C#

  str = textBox23.Text; 
      retString = str.Substring(1, 1); 
      textBox22.Text = retString; 

我猜測if/else寫得不好,但我已經檢查了不同的變量,但沒有奏效。我真的不知道發生了什麼 下面的代碼:

private void button10_Click_1(object sender, EventArgs e) 
{ 

    if (textBox23.Text == "" || Int32.Parse(textBox23.Text) > 9) 

    { 
     double L1 = double.Parse(textBox13.Text); 
     double L2 = double.Parse(textBox16.Text); 
     double wynik = L1 - L2; 
     textBox23.Text = wynik.ToString(); 

     string str = null; 
     string retString = null; 
     str = textBox23.Text; 
     retString = str.Substring(0, 1); 
     textBox21.Text = retString; 

     str = textBox23.Text; 
     retString = str.Substring(1, 1); 
     textBox22.Text = retString; 

    } 

    else if (textBox23.Text == "" || Int32.Parse(textBox23.Text) 
    < 10) 
    { 
     double L1 = double.Parse(textBox13.Text); 
     double L2 = double.Parse(textBox16.Text); 
     double wynik = L1 - L2; 
     textBox23.Text = wynik.ToString(); 

     string str = null; 
     string retString = null; 
     str = textBox23.Text; 
     retString = str.Substring(0, 1); 
     textBox21.Text = retString; 

    } 
+0

你要什麼做的,如果文字是空白的?谷歌翻譯 –

+0

還不完全。 – Erix

+0

問題是什麼?我不明白。 – gabsferreira

回答

2

你改變的textbox23

textBox23.Text = wynik.ToString(); 

任何機會,這個值的心不是2個字符長的值,因爲後者你做

str = textBox23.Text; 
retString = str.Substring(1, 1); 

由於子字符串是從零開始的起始字符,所以如果至少2個字符不成立,您將得到一個錯誤

0

可以使用的TryParse

private void button10_Click_1(object sender, EventArgs e) 
{ 
    Int32 IsAnumber; 
if(Int32.TryParse(textBox23.Text, out IsAnumber) 
{ 
if (IsAnumber > 9) 

{ 
    double L1 = double.Parse(textBox13.Text); 
    double L2 = double.Parse(textBox16.Text); 
    double wynik = L1 - L2; 
    textBox23.Text = wynik.ToString(); 

    string str = null; 
    string retString = null; 
    str = textBox23.Text; 
    retString = str.Substring(0, 1); 
    textBox21.Text = retString; 

    str = textBox23.Text; 
    retString = str.Substring(1, 1); 
    textBox22.Text = retString; 

} 

else 
{ 
    double L1 = double.Parse(textBox13.Text); 
    double L2 = double.Parse(textBox16.Text); 
    double wynik = L1 - L2; 
    textBox23.Text = wynik.ToString(); 

    string str = null; 
    string retString = null; 
    str = textBox23.Text; 
    retString = str.Substring(0, 1); 
    textBox21.Text = retString; 

} 
} 
0

尚不清楚你想要得到自己,但是我會去這個

int int23 = 0; 

if (!int.TryParse(textBox23.Text, out int23)) 
{ 
    // manage error and exit function (via exception or return), test in here also string.Empty for textBox23. 
} 

if (int23 > 9) 
{ 
    // Do What you need 
} 
else if (int23 < 10) 
{ 
    // Do What you need 
} 

然而,一旦你得到了INT變量,就可以直接進行格式化。你正在努力實現

0

像這樣的事情看起來類似於

private void button10_Click_1(object sender, EventArgs e) 
    { 
     double box23; 

     if (double.TryParse(textBox23.Text, out box23)) 
     { 
      double L1 = double.Parse(textBox13.Text); 
      double L2 = double.Parse(textBox16.Text); 
      double wynik = L1 - L2; 
      textBox23.Text = wynik.ToString(); 

      string str = textBox23.Text; 
      string retString = str.Substring(0, 1); 
      textBox21.Text = retString; 

      if (box23 > 9) 
      { 
       str = textBox23.Text; 
       retString = str.Substring(1, 1); 
       textBox22.Text = retString; 
      } 
     } 
    }