Цель состоит в том, чтобы "Возвращаемая переменная работала" в переменную.
Я пытаюсь избежать использования любых глобальных переменных, таких как $ 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