2014-09-13 55 views
-1

我的問題是當用戶單擊myButton時,程序運行得非常好。但是,如果用戶在第一個文本框中輸入的值小於3,則用戶將看到一個消息框,指出該值必須大於3米。如果單擊確定,myButton中的下一個方法仍然運行,並且結果消息框出現。防止其他方法在以前方法中滿足的條件下運行

我試過四處尋找解決使用嵌套for循環的這個問題,但未能讓他們工作(很可能是我的錯誤)。我也不喜歡使用Goto,因爲它不是很好的編程習慣。當然你可以告訴我,否則如果你想:)。

// Button 
private void myButton_Click(object sender, EventArgs e) 
     { 
      checkIfNumericalValue(); 
      testIfTextBoxOnesMinimumIsMet(); 
      testIfTextBoxTwosMinimumIsMet(); 
      displayResultToUser(); 
      resetOrClose(); 
     } 

// Textbox One 
public void testIfTextBoxOnesMinimumIsMet() 
     { 
      if (length < 3) 
      { 
       MessageBox.Show("length must be greater than 3 metres"); 
      } 
     } 

幫助將不勝感激,這也是在C#中的Visual Studio 2012年我第二次嘗試,不要擔心,這無關我的10年教育作爲我的學校沒有一個編程問題。此問題發生在testIfTextBoxOnesMinimumIsMet()和testIfTextBoxOnesMinimumIsMet()以及如果有人可以幫助我用這一個方法,我應該能夠修復其餘的:)

回答

1

你可以從你的內部函數throwexception並從按鈕的功能抓住它,像這樣:

// Button 
private void myButton_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     checkIfNumericalValue(); 
     testIfTextBoxOnesMinimumIsMet(); 
     testIfTextBoxTwosMinimumIsMet(); 
     displayResultToUser(); 
     resetOrClose(); 
    } 
    catch (ArgumentException ex) 
    { 
     // The error message we defined at the exception we threw 
     MessageBox.Show(ex.Message); 
    } 
} 

// Textbox One 
public void testIfTextBoxOnesMinimumIsMet() 
{ 
    if (length < 3) 
    { 
     throw new ArgumentException("Length must be greater than 3 meters."); 
    } 
} 

另一種方法是你的按鈕內處理驗證,像這樣:

// Button 
private void myButton_Click(object sender, EventArgs e) 
{ 
    checkIfNumericalValue(); 
    if (length < 3) 
    { 
     MessageBox.Show("Length must be greater than 3 meters."); 
     return; 
    } 
    testIfTextBoxTwosMinimumIsMet(); 
    displayResultToUser(); 
    resetOrClose(); 
} 

什麼上面發生的是return將離開該功能無需進一步處理別的。

+0

看起來很簡單:P返回;離開該功能,並不繼續處理任何進一步是非常有益的。謝謝你的這兩個例子:) – AsDf 2014-09-13 09:44:38

1

看來,你需要一些其他變量來跟蹤是否你遇到錯誤。爲此,您可以定義一個bool noErrors變量,並且如果沒有錯誤,則應該從錯誤檢查方法返回一個布爾值,即True,否則返回False。這樣你就知道你是否遇到過任何問題。

最後,在運行任何其他方法之前,您應該檢查errrorsFound的狀態。

例如:

// Button 
private void myButton_Click(object sender, EventArgs e) 
{ 
    bool noErrors = 
     isNumericalValue() && 
     textBoxOnesMinimumIsMet() && 
     textBoxTwosMinimumIsMet(); 

    if (noErrors) 
    { 
     displayResultToUser(); 
     resetOrClose(); // I'm not sure if this should happen regardless of errors? 
    }   
} 

// Textbox One 
public bool textBoxOnesMinimumIsMet() 
{ 
    if (length < 3) 
    { 
     MessageBox.Show("length must be greater than 3 metres"); 
     return false; 
    } 

    return true; 
} 
+0

isNumericalValue是一個私人布爾,所以我不幸使用&&操作符:( – AsDf 2014-09-13 08:07:17

+0

@AsDf你的意思是私人無效?因爲如果它的私人布爾比它可以使用是的。 – Prix 2014-09-13 20:07:18

1

所以,如果我理解正確這一點,如果文本框中包含數值,文本框1滿足最低和文本框2滿足最低,要displayResultToUser ()然後resetOrClose()。

如果是這樣的話,你可以有3種方法checkIfNumericalValue(),testIfTextBoxOnesMinimumIsMet()和testIfTextBoxTwosMinimumIsMet()返回一個布爾值取決於最起碼的條件是什麼,然後寫些這樣的:

private void myButton_Click(object sender, EventArgs e) 
{ 
    if (checkIfNumericalValue() && testIfTextBoxOnesMinimumIsMet(Convert.ToInt32(txtBoxOne.Text)) && testIfTextBoxTwosMinimumIsMet(Convert.ToInt32(txtBoxTwo.Text))) 
    { 
     displayResultToUser(); 
     resetOrClose(); 
    } 
} 

public bool testIfTextBoxOnesMinimumIsMet(int length) 
{ 
    if (length < 3) 
    { 
     MessageBox.Show("length must be greater than 3 metres"); 
     return false; 
    } 

    return true; 
} 
相關問題