[英]Windows Form can't access PowerShell variable
我正在嘗試在Windows窗體中顯示PowerShell變量。 我在表格上有一個按鈕和一個標簽。 我可以在標簽上顯示TestFunction返回的字符串,但不能在Form彈出窗口中顯示。
這是我的PowerShell腳本:
function TestFunction()
{
return "PASSED"
}
Function Button_Click()
{
$testResults = TestFunction
$ResultLabel1.Text = $testResults ## this works - I can see "PASSED"
$TroubleButton1.Add_Click(
{
# This does not work
[System.Windows.Forms.MessageBox]::Show($testResults)
}
)
}
## Call function
Button_Click
為什么表單認為$ testResults為NULL?
您在Button_Click
范圍中分配$testResults
變量,因此當您離開該范圍時,您將丟失該變量。 您應該將變量保存在范圍內,當按下$TroubleButton1
按鈕時,該范圍將存在。 或者您可以從$ResultLabel1
標簽而不是變量中選擇值:
[System.Windows.Forms.MessageBox]::Show($ResultLabel1.Text)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.