Получить возвращаемую переменную из события add_Click (PowerShell) - PullRequest
0 голосов
/ 07 марта 2019

Цель состоит в том, чтобы "Возвращаемая переменная работала" в переменную. Я пытаюсь избежать использования любых глобальных переменных, таких как $ Script: $ Global: Out-host не вариант. В идеале функция 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 голосов
/ 07 марта 2019

Использовать хеш-таблицу:

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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...