在修改后的脚本下方继续我的评论
$x = # the return variable which is updated in the OK Click event
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Combobox"
$objForm.Size = New-Object System.Drawing.Size(300,200)
$objForm.StartPosition = "CenterScreen"
$objForm.KeyPreview = $True
$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.DialogResult = [System.Windows.Forms.DialogResult]::OK
$OKButton.Add_Click({
$script:x = $objCombobox.SelectedItem # there is only one SelectedItem
$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.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$objForm.Controls.Add($CancelButton)
# using this, you do not need the Add_KeyDown events to react on clicking the OK or Cancel button
$objForm.AcceptButton = $OKButton
$objForm.CancelButton = $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 choose"
$objForm.Controls.Add($objLabel)
$objCombobox = New-Object System.Windows.Forms.Combobox
$objCombobox.Location = New-Object System.Drawing.Size(10,40)
$objCombobox.Size = New-Object System.Drawing.Size(260,20)
$objCombobox.Height = 70
[void] $objCombobox.Items.Add("Item 1")
[void] $objCombobox.Items.Add("Item 2")
[void] $objCombobox.Items.Add("Item 3")
[void] $objCombobox.Items.Add("Item 4")
[void] $objCombobox.Items.Add("Item 5")
$objCombobox.Add_SelectedIndexChanged({
# for demo, just write to console
Write-Host "You have selected '$($this.SelectedItem)'" -ForegroundColor Cyan
# inside here, you can refer to the $objCombobox as $this
switch ($this.SelectedItem) {
"Item 1" { <# do something when Item 1 is selected #> }
"Item 2" { <# do something when Item 2 is selected #> }
"Item 3" { <# do something when Item 3 is selected #> }
"Item 4" { <# do something when Item 4 is selected #> }
"Item 5" { <# do something when Item 5 is selected #> }
}
})
$objForm.Controls.Add($objCombobox)
$objForm.Topmost = $True
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
# IMPORTANT clean up the form when done
$objForm.Dispose()
$x
如您所见,我已经删除了两个$objForm.Add_KeyDown() 事件处理程序,因为当您设置$objForm.AcceptButton = $OKButton 和$objForm.CancelButton = $CancelButton 时不需要它们。此外,使用$objForm.Dispose()
完成后,请始终从内存中删除表单
根据您的评论,我了解您只想在关闭表单后执行所选操作。
在这种情况下,还要删除 $OKButton.Add_Click() 事件处理程序,并将所选选项存储在 SelectedIndexChanged 事件中的 $script:x 中:
$x = # the return variable which is updated in the SelectedIndexChanged event handler
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Combobox"
$objForm.Size = New-Object System.Drawing.Size(300,200)
$objForm.StartPosition = "CenterScreen"
$objForm.KeyPreview = $True
$objForm.Topmost = $True
$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.DialogResult = [System.Windows.Forms.DialogResult]::OK
$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.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$objForm.Controls.Add($CancelButton)
# using this, you do not need the Add_KeyDown events to react on clicking the OK or Cancel button
$objForm.AcceptButton = $OKButton
$objForm.CancelButton = $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 choose"
$objForm.Controls.Add($objLabel)
$objCombobox = New-Object System.Windows.Forms.Combobox
$objCombobox.Location = New-Object System.Drawing.Size(10,40)
$objCombobox.Size = New-Object System.Drawing.Size(260,20)
$objCombobox.Height = 70
[void] $objCombobox.Items.Add("Item 1")
[void] $objCombobox.Items.Add("Item 2")
[void] $objCombobox.Items.Add("Item 3")
[void] $objCombobox.Items.Add("Item 4")
[void] $objCombobox.Items.Add("Item 5")
$objCombobox.Add_SelectedIndexChanged({
# inside here, you can refer to the $objCombobox as $this
# set the return variable to the SelectedItem
$script:x = $this.SelectedItem # there is only one SelectedItem
# or, if you want it to, you can start the action here immediately without leaving the form:
# switch ($this.SelectedItem) {
# "Item 1" { <# do something when Item 1 is selected #> }
# "Item 2" { <# do something when Item 2 is selected #> }
# "Item 3" { <# do something when Item 3 is selected #> }
# "Item 4" { <# do something when Item 4 is selected #> }
# "Item 5" { <# do something when Item 5 is selected #> }
# }
})
$objForm.Controls.Add($objCombobox)
$objForm.Add_Shown({$objForm.Activate()})
$dialogResult = $objForm.ShowDialog()
# IMPORTANT clean up the form when done
$objForm.Dispose()
if ($dialogResult -eq 'OK' -and ![string]::IsNullOrWhiteSpace($x)) {
# the form was not cancelled and a selection was made
# for demo, just write to console
Write-Host "You have selected '$($x)'" -ForegroundColor Cyan
switch ($x) {
"Item 1" { <# do something when Item 1 is selected #> }
"Item 2" { <# do something when Item 2 is selected #> }
"Item 3" { <# do something when Item 3 is selected #> }
"Item 4" { <# do something when Item 4 is selected #> }
"Item 5" { <# do something when Item 5 is selected #> }
}
}
else {
Write-Host "You have cancelled the dialog or did not select anything"
}
好的,显然现在您希望表单在做出选择并单击OK 按钮(并因此执行所选操作)后保持不变。
在这种情况下,您可以这样简化:
$x = @() # the return variable (an array with all selections made in order)
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Combobox"
$objForm.Size = New-Object System.Drawing.Size(300,200)
$objForm.StartPosition = "CenterScreen"
$objForm.KeyPreview = $True
$objForm.Topmost = $True
$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({
$selected = $objCombobox.SelectedItem
$script:x += $objCombobox.SelectedItem
if (![string]::IsNullOrWhiteSpace($selected)) {
Write-Host "Performing action $($selected)" -ForegroundColor Yellow
$objLabel.Text = "Performing action $($selected)"
switch ($selected) {
"Item 1" { <# do something when Item 1 is selected #> }
"Item 2" { <# do something when Item 2 is selected #> }
"Item 3" { <# do something when Item 3 is selected #> }
"Item 4" { <# do something when Item 4 is selected #> }
"Item 5" { <# do something when Item 5 is selected #> }
}
$objLabel.Text = "Please choose"
$objCombobox.SelectedIndex = -1 # reset the combobox to blank
}
})
$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.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$objForm.Controls.Add($CancelButton)
# using this, you do not need the Add_KeyDown events to react on clicking the OK or Cancel button
$objForm.AcceptButton = $OKButton
$objForm.CancelButton = $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 choose"
$objForm.Controls.Add($objLabel)
$objCombobox = New-Object System.Windows.Forms.Combobox
$objCombobox.Location = New-Object System.Drawing.Size(10,40)
$objCombobox.Size = New-Object System.Drawing.Size(260,20)
$objCombobox.Height = 70
[void] $objCombobox.Items.Add("Item 1")
[void] $objCombobox.Items.Add("Item 2")
[void] $objCombobox.Items.Add("Item 3")
[void] $objCombobox.Items.Add("Item 4")
[void] $objCombobox.Items.Add("Item 5")
$objForm.Controls.Add($objCombobox)
$objForm.Add_Shown({$objForm.Activate()})
$dialogResult = $objForm.ShowDialog()
# IMPORTANT clean up the form when done
$objForm.Dispose()
if ($dialogResult -ne 'Cancel' -or $x.Count -eq 0) {
# the form was cancelled or no selection was made
# for demo, just write to console
Write-Host "You have cancelled the dialog or did not select anything.."
}
else {
Write-Host "The selection(s) you made were $($x -join ', ')" -ForegroundColor Cyan
}