1

I have an if statement that checks if a textbox is not empty. HOwever, if it True, meaning empty i want it to cancel the rest of the process and go back to my form. Below is the IF statement that i have, i cant figure out how to Cancel the remainder of the process.

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

2 回答 2

2

Calling a method like

DoSomething();

causes it to start executing whatever is inside. In some point, if you no longer wish to continue in execution of that method call, use return statement with no return value for methods returning void or return something for methods with non-void return type, where something is type of the return type.

public void DoSomething()
{
   ... do something
   if (condition)
      return; // returns from a method call
}
于 2013-11-03T06:43:40.707 回答
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();
        }
    }
}
于 2013-11-03T06:08:38.140 回答