title: How can you exit a Windows Forms application from within an event handler function?
body: <p>I am writing a powershell script to backup and restore databases.  I am using forms to gather user input.  At any point in the information gathering process, the user can hit "cancel".</p>
  
  <p>To keep things tidy, I moved the form logic into separate functions.  Is there a way to "exit" the entire script from within the function?</p>
  
  <p>I tried putting the exit command inside the add_click event, but I get an exception error.</p>
  
  <pre><code>  # create Cancel button
    $CancelButton = New-Object System.Windows.Forms.Button
    $CancelButton.Location = New-Object System.Drawing.Size(160,320)
    $CancelButton.Size = New-Object System.Drawing.Size(75,23)
    $CancelButton.Text = "Cancel"
    $CancelButton.Add_Click(
    {$Looping=$False
     $RestoreFromFileForm.Close()
     exit
    })
    $RestoreFromFileForm.Controls.Add($CancelButton)
  </code></pre>