powershell添加动作到组合框



目前,我想构建一个powershell gui。表格运行正常,所以没有问题。现在我想为我的选择添加一个事件,例如启动另一个脚本选择"item1"另一个脚本选择item2;等等......我怎样才能做到呢?谢谢你的帮助。这是我实际拥有的。

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("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.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
{
foreach ($objItem in $objCombobox.SelectedItem)
{$x += $objItem}
$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(
{
foreach ($objItem in $objCombobox.SelectedItem)
{$x += $objItem}
$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 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) 
[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.Height = 70
$objForm.Controls.Add($objCombobox) 
$objForm.Topmost = $True
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
$x

继续我的评论,在修改后的脚本下面

$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()事件处理程序,只是将$script:x中选择的选项存储在SelectedIndexChanged事件中:

$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,显然现在您希望表单在做出选择并单击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
}

最新更新