从add_Click事件(PowerShell)获取返回的变量

时间:2019-03-07 03:09:47

标签: winforms powershell

目标是将“返回的变量有效”转化为变量。 我试图避免使用任何全局变量,例如$ Script:$ Global: 脱离主机是不可选项。理想情况下,“ Generate-Form”函数可以返回变量。

    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

1 个答案:

答案 0 :(得分:0)

使用哈希表:

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