0

我在这里挣扎。

拿这个示例代码:

[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(500,400) 
$objForm.StartPosition = "CenterScreen"

$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
    {$output1=$objTextBox.Text}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") 
    {$objForm.Close()}})

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(100,90)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "Merge"
$OKButton.Add_Click({$output=$objTextBox.Text})
$objForm.Controls.Add($OKButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(300,90)
$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(160,20) 
$objLabel.Size = New-Object System.Drawing.Size(280,20) 
$objLabel.Text = "Please enter the target machine: "
$objForm.Controls.Add($objLabel) 

$output = new-object System.Windows.forms.label
$output.location = new-object System.drawing.size(30, 140)
$output.size = New-Object System.Drawing.Size(300,300) 
$objform.controls.add($output)

$objTextBox = New-Object System.Windows.Forms.TextBox 
$objTextBox.Location = New-Object System.Drawing.Size(115,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()
$output

我一生都无法弄清楚为什么当我在框中输入文本并单击 OKButton (Merge) 时没有任何反应。它应该输出到 $output,或者至少,这就是我想要发生的事情。

我只是想让代码获取文本框的输入并将其输出到名为 $output 的标签表单。

从某种意义上说,当我将输入输入文本框并按“合并”时,它可以工作,它会将其输出到 Powershell_ISE 控制台,但只有在窗口关闭并第二次运行之后(它不是当我按下“合并”时刷新 $output 标签,并且仅在我关闭程序时输出)

请帮忙!

谢谢

4

1 回答 1

2

调整

$OKButton.Add_Click({$output=$objTextBox.Text})

$OKButton.Add_Click({$output.text=$objTextBox.Text})
于 2012-04-19T07:57:03.053 回答