通过确认按钮提取组合框文本

Extract Combo Box text via confirmation button

完全公开,我是 powershell 的新手。我创建了一个脚本,要求用户从 PC 上的一组本地用户通过 Powershell 中的组合框 select。我还创建了一个按钮来确认所说 selection。我的问题是,当用户单击确认时,我希望组合框中的文本(不一定指 PC 上的本地用户)由 $User 变量表示。因此,下拉菜单,select 用户,单击确认,现在我的脚本的其余部分将 运行 组合框文本表示为 $User.

现在我有一个标记为 $OutputBox 的文本框,以查看它是否正在导入组合框文本;不需要。我已经尝试了 $Button.add_click{$DropdownBox.text = $User})、$DropdownBox.SelectedItem = $User 和其他一些没有预期结果的组合。提前感谢您的帮助。

Add-type -AssemblyName System.Windows.Forms
Add-type -AssemblyName System.Drawing

$Form = New-Object System.Windows.Forms.Form
$Form.Size = New-Object System.Drawing.Size(600,400)

### At this point all the Assembly is a 600x400 pixel box. Next line creates the drop down menu, aka ComboBox in the .NET forms. 

$DropDownBox = New-Object System.Windows.Forms.ComboBox
$DropDownBox.Location = New-Object System.Drawing.Size(20,50)
$DropDownBox.Size = New-Object System.Drawing.Size(180,20)
$DropDownBox.Height = 400

$CancelBtn = New-Object System.Windows.Forms.Button
$CancelBtn.Text = "Cancel"
$CancelBtn.Size = New-Object System.Drawing.Size(75,75)
$CancelBtn.Location = New-Object System.Drawing.Point(260,250)
$CancelBtn.add_click({$form.tag = $form.Close()})

$Details = Get-ChildItem -Path C:\Users -Directory -Exclude default*, ntsetup, thsystem, -Force -ErrorAction SilentlyContinue | Select-Object -ExpandProperty name
foreach($Detail in $Details){
$DropDownBox.Items.Add($Detail)
$DropDownBox.Sorted = $true
}

$Button = New-Object System.Windows.Forms.Button
$button.Location = New-Object System.Drawing.Size(400,100)
$Button.Size = New-Object System.Drawing.Size(80,50)
$Button.Text = "Confirm Selection"

$OutputBox = New-Object System.Windows.Forms.TextBox
$OutputBox.Location = New-Object System.Drawing.size(420,50)
$OutputBox.Size = New-Object System.Drawing.Size(100,20)
$OutputBox.Height = 100

$form.Controls.Add($DropDownBox)
$Form.Controls.Add($OutputBox)
$Form.controls.add($Button)
$form.controls.add($cancelbtn)

$Button.add_click({})

[void]$form.ShowDialog() 

你是在说这个吗...

Add-type -AssemblyName System.Windows.Forms
Add-type -AssemblyName System.Drawing

$Form      = New-Object System.Windows.Forms.Form
$Form.Size = New-Object System.Drawing.Size(600,400)

<#
At this point all the Assembly is a 600x400 pixel box. Next line creates the 
drop down menu, aka ComboBox in the .NET forms. 
#>

$DropDownBox           = New-Object System.Windows.Forms.ComboBox
$DropDownBox.Location  = New-Object System.Drawing.Size(20,50)
$DropDownBox.Size      = New-Object System.Drawing.Size(180,20)
$DropDownBox.Height    = 400

$CancelBtn                      = New-Object System.Windows.Forms.Button
$CancelBtn.Text                 = 'Cancel'
$CancelBtn.Size                 = New-Object System.Drawing.Size(75,75)
$CancelBtn.Location             = New-Object System.Drawing.Point(260,250)
$CancelBtn.add_click({$form.tag = $form.Close()})

$Details = Get-ChildItem -Path C:\Users -Directory -Exclude default*, ntsetup, 
           thsystem, -Force -ErrorAction SilentlyContinue | 
           Select-Object -ExpandProperty name

foreach($Detail in $Details)
{
    $DropDownBox.Items.Add($Detail)
    $DropDownBox.Sorted = $true
}

$Button             = New-Object System.Windows.Forms.Button
$button.Location    = New-Object System.Drawing.Size(400,100)
$Button.Size        = New-Object System.Drawing.Size(80,50)
$Button.Text        = 'Confirm Selection'

$OutputBox          = New-Object System.Windows.Forms.TextBox
$OutputBox.Location = New-Object System.Drawing.size(420,50)
$OutputBox.Size     = New-Object System.Drawing.Size(100,20)
$OutputBox.Height   = 100

$form.Controls.Add($DropDownBox)
$Form.Controls.Add($OutputBox)
$Form.controls.add($Button)
$form.controls.add($cancelbtn)

$Button.add_click({$OutputBox.Text = $DropDownBox.Text})

[void]$form.ShowDialog()