我在这里挣扎。
获取此示例代码:
[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标签,只有当我关闭程序时才输出)
请帮忙!
谢谢
答案 0 :(得分:2)
修改
$OKButton.Add_Click({$output=$objTextBox.Text})
到
$OKButton.Add_Click({$output.text=$objTextBox.Text})