2013-11-03 17 views
0

我有一個if語句來檢查文本框是否爲空。 HOwever,如果它是真的,意思是空的,我希望它取消其餘的過程並回到我的表單。下面是我有的IF聲明,我無法弄清楚如何取消剩下的過程。C#if語句true取消剩餘的進程

if (textBox2.Text.Equals("")) 
{ 
    MessageBox.Show("Field is Empty", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); 
} 
+2

你想取消什麼過程? –

+0

「我希望它取消過程的其餘部分並返回到我的表單」您現在在哪裏? –

+0

你的過程是什麼?你只是顯示一個If語句。你想要退出/取消什麼? –

回答

1

調用像

的方法
DoSomething(); 

使其開始執行任何在裏面。在某些情況下,如果不再希望繼續執行該方法調用,請使用return語句,對於返回void的方法沒有返回值,對於具有非void返回類型的方法,使用return something,其中something是返回類型的類型。

public void DoSomething() 
{ 
    ... do something 
    if (condition) 
     return; // returns from a method call 
} 
1

http://msdn.microsoft.com/fr-fr/library/1dac1663%28v=vs.80%29.aspx

private void validateUserEntry2() 
{ 
    // Checks the value of the text. 
    if(serverName.Text.Length == 0) 
    { 
     // Initializes the variables to pass to the MessageBox.Show method. 

     string message = "You did not enter a server name. Cancel this operation?"; 
     string caption = "No Server Name Specified"; 
     MessageBoxButtons buttons = MessageBoxButtons.YesNo; 
     DialogResult result; 

     // Displays the MessageBox. 

     result = MessageBox.Show(this, message, caption, buttons, 
      MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, 
      MessageBoxOptions.RightAlign); 

     if(result == DialogResult.Yes) 
     { 
      // Closes the parent form. 
      this.Close(); 
     } 
    } 
}