title: PowerShell Cancel Button Stop Script
body: <p>I am having trouble with some PS GUI functionality. </p>
  
  <p>I have tried quite a few things and have been looking everywhere (ALL THE LINKS ARE PURPLE!).</p>
  
  <p>I have a cancel button on my form, right now when the user clicks the cancel button, it points to this handler, and my form will close.</p>
  
  <pre><code>  $handler_cancelButton_Click={
        $MainForm.Close()
    }
  </code></pre>
  
  <p>However, once the form closes, all of the rest of my script tries to keep running (FTP, run Excel, sends emails, etc.) and throws me errors.</p>
  
  <p>I have tried many different things, I have ran just the <code>Exit</code> command and that closes my form, throws an error, and closes down my entire IDE (PowerGUI). </p>
  
  <p>Now I have also tried things like this:</p>
  
  <p><code>[environment]::exit(0)</code></p>
  
  <p><code>[System.Windows.Forms.Application]::Exit($null)</code> </p>
  
  <p>Both, will do exactly what calling just <code>Exit</code> will do. Close my form, and PowerGUI. </p>
  
  <p>What I want to happen is when the user presses cancel, my form closes, and the rest of my script stops or terminates. </p>
  
  <p><strong>EDIT</strong> </p>
  
  <p>Now once I have a GUI.ps1, and a Code.ps1. I guess I would call GUI.ps1 inside  of Code.ps1, get the user input that I need into variables. Then take those variables and pass them from GUI.ps1, over to Code.ps1 (I need that info in order to run anything here). </p>
  
  <p>Then once I have those values in Code.ps1, run everything else I am doing..? </p>
  
  <p>But now if my user decided to only hit the cancel button for some reason...</p>
  
  <p>(likely to not happen a lot to be honest, I more so want to get this functionality working for learning sake. They might accidentally run the script, somehow..)</p>
  
  <p>...it would just close the GUI, but wouldn't the rest of Code.ps1 still run?</p>
  
  <p>Or now would I be able to say something like this in my Code.ps1 script:</p>
  
  <pre><code>if($someGUIValue -eq $null -and $someOtherGUIValues - eq $null){
      Exit   #would I be able to run this now the way I think it would?
  }
  else{
      #yes, run the script..
  }
  </code></pre>
  
  <p>*<strong>*FOUND ANSWER</strong> </p>
  
  <p><strong>EDIT</strong>..</p>
  
  <p>What I did was I created a flag. Before any of my script had started I declared a variable like so</p>
  
  <p><code>$terminateScript = $false</code></p>
  
  <p>Within my event handler I have added this</p>
  
  <pre><code>$handler_cancelButton_Click={
      $terminateScript = $true
      $MainForm.Close()
      return $terminateScript 
  }
  </code></pre>
  
  <p>Once they hit the cancel button it will set <code>$terminateScript = $true</code>. So now where I am calling all of my functions I have something like this:</p>
  
  <pre><code>$username, $password, $someOtherValue, $anotherVaklue, $terminateScript = MainForm
  if($terminateScript -eq $false){
      SomeFunction
      AnotherFunction
  }
  else{
       Exit #this now works here, and cancels my script, without closing my IDE.
  }
  </code></pre>
  
  <p>Thanks for the help</p>