0

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.

    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
Aaron
  • 563
  • 3
  • 13
  • Possible duplicate of [Using form to return variable fails to produce a return value](https://stackoverflow.com/questions/39285860/using-form-to-return-variable-fails-to-produce-a-return-value) – TessellatingHeckler Mar 07 '19 at 03:26

1 Answers1

0

Use a hashtable:

Add-Type -AssemblyName System.Windows.Forms    
Add-Type -AssemblyName System.Drawing

$hash = [hashtable]::Synchronized(@{}) 
$hash.returnedVar = ""

Function Button_Click() {

    [System.Windows.Forms.MessageBox]::Show("Button Clicked")
    $returnedVariable = "The Returned Variable Worked"
    Return $returnedVariable
}

Function Generate-Form {

    # 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({$hash.returnedVar = Button_Click; [void]$form.Close(); [void]$form.Dispose(); })
    # $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 
    [void]$form.ShowDialog() 
    $OutputVariableFromGenerateForm # This is OK
    $hash.returnedVar # This is "The Returned Variable Worked"

} #End Function 

Generate-Form
f6a4
  • 1,684
  • 1
  • 10
  • 13
  • [hashtable]::Synchronized(@{}) is global. It can cross runspaces. My goal is make it easier to debug by removing variables that cross different scopes. – Aaron Mar 07 '19 at 07:15
  • " $OutputVariableFromGenerateForm # This is OK". $OutputVariableFromGenerateForm does not return anything in Powershell ISE. How did you run the script? – Aaron Mar 07 '19 at 07:24