賦值給變量使用微軟的Technet例子在這裏和下面無法從Powershell的形式
https://technet.microsoft.com/en-us/library/ff730941.aspx?f=255&MSPPError=-2147217396
粘貼我不能讓它用戶輸入實際賦給變量$x
我已經嘗試刪除該行末尾[void] $objForm.ShowDialog()
,但它似乎總是返回取消文本
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Data Entry Form"
$objForm.Size = New-Object System.Drawing.Size(300,200)
$objForm.StartPosition = "CenterScreen"
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
{$x=$objTextBox.Text;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
{$objForm.Close()}})
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({$x=$objTextBox.Text;$objForm.Close()})
$objForm.Controls.Add($OKButton)
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20)
$objLabel.Size = New-Object System.Drawing.Size(280,20)
$objLabel.Text = "Please enter the information in the space below:"
$objForm.Controls.Add($objLabel)
$objTextBox = New-Object System.Windows.Forms.TextBox
$objTextBox.Location = New-Object System.Drawing.Size(10,40)
$objTextBox.Size = New-Object System.Drawing.Size(260,20)
$objForm.Controls.Add($objTextBox)
$objForm.Topmost = $True
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
$x
我剛剛意識到這個頁面上的代碼非常相似,工作正常,但我不明白爲什麼第一的Technet例子沒有。可能是因爲它是PS 1.0? https://msdn.microsoft.com/en-us/powershell/scripting/getting-started/cookbooks/creating-a-custom-input-box – benji1304
是否改變'$ X ='在事件處理程序'$全球: x ='修理東西? –
我會試試。進一步閱讀指出,這是一個常見問題,因爲原始代碼已過時,並且不能通過3.0。 https://blogs.technet.microsoft.com/heyscriptingguy/2014/08/02/weekend-scripter-fixing-powershell-gui-examples/ – benji1304