title: Get returned variable from add_Click event (PowerShell)
body: <p>The goal is to get the "The Returned Variable Worked" into a variable.
  I am trying to avoid the using any globals such as $Script: $Global:
  Out-host is not an option. Ideally the "Generate-Form" function could return the variable.</p>
  
  <pre><code>    Function Button_Click()
      {
          [System.Windows.Forms.MessageBox]::Show("Button Clicked")
          $returnedVariable = "The Returned Variable Worked"
          Return $returnedVariable
      }
  
      Function Generate-Form {
  
          Add-Type -AssemblyName System.Windows.Forms    
          Add-Type -AssemblyName System.Drawing
  
          # Build Form
          $Form = New-Object System.Windows.Forms.Form
          $Form.Text = "My Form"
          $Form.Size = New-Object System.Drawing.Size(200,200)
          $Form.StartPosition = "CenterScreen"
          $Form.Topmost = $True
  
          # Add Button
          $Button = New-Object System.Windows.Forms.Button
          $Button.Location = New-Object System.Drawing.Size(35,35)
          $Button.Size = New-Object System.Drawing.Size(120,23)
          $Button.Text = "Show Dialog Box"
  
          $Form.Controls.Add($Button)
  
          #Add Button event 
  
          $OutputVariableFromGenerateForm = $Button.Add_Click({$returnedVar = Button_Click ; $Form.Close(); return $returnedVar})
          # $returnedVar contains an array @("OK,"The Returned Variable Worked"), 
          # but it appears to be out of scope because it is in a script block.
          # I only want "The Returned Variable Worked"
  
  
          #Show the Form 
          $form.ShowDialog()| Out-Null
          $OutputVariableFromGenerateForm # This is null
          $returnedVar # This is null
  
      } #End Function 
  
      Generate-Form
  </code></pre>