Search code examples
c#messageboxisnullorempty

C# if statement true cancel the remaining process


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);
}

Solution

  • 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
    }