powershell将操作添加到组合框
目前,我想构建一个powershell gui。表单运行良好,因此没有问题。现在我想为我的选择添加一个事件,例如启动另一个脚本选择“item1”,另一个脚本选择“item2”,等等。我怎样才能做到这一点?谢谢你的帮助。 这就是我实际拥有的powershell将操作添加到组合框,powershell,Powershell,目前,我想构建一个powershell gui。表单运行良好,因此没有问题。现在我想为我的选择添加一个事件,例如启动另一个脚本选择“item1”,另一个脚本选择“item2”,等等。我怎样才能做到这一点?谢谢你的帮助。 这就是我实际拥有的 [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") [void] [System.Reflection.Assembly]::Lo
[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
好的,显然现在您希望表单在做出选择并单击
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
}
$x=@()#返回变量(按顺序进行所有选择的数组)
添加类型-AssemblyName System.Windows.Forms
添加类型-AssemblyName System.Drawing
$objForm=新对象System.Windows.Forms.Form
$objForm.Text=“组合框”
$objForm.Size=新对象系统.Drawing.Size(300200)
$objForm.StartPosition=“中心屏幕”
$objForm.KeyPreview=$True
$objForm.Topmost=$True
$OKButton=新建对象System.Windows.Forms.Button
$OKBUTON.Location=新对象系统.图纸.尺寸(75120)
$OKBUTON.Size=新对象系统.Drawing.Size(75,23)
$OKButton.Text=“确定”
$OK按钮。添加\单击({
$selected=$objCombobox.SelectedItem
$script:x+=$objCombobox.SelectedItem
if(![string]::IsNullOrWhiteSpace($selected)){
写入主机“正在执行操作$($已选择)”-ForegroundColor黄色
$objLabel.Text=“正在执行操作$($selected)”
交换机($selected){
“项目1”{}
“项目2”{}
“项目3”{}
“项目4”{}
“项目5”{}
}
$objLabel.Text=“请选择”
$objCombobox.SelectedIndex=-1#将组合框重置为空白
}
})
$objForm.Controls.Add($OKButton)
$CancelButton=新建对象System.Windows.Forms.Button
$CancelButton.Location=新对象系统.图纸.尺寸(150120)
$CancelButton.Size=新对象系统.Drawing.Size(75,23)
$CancelButton.Text=“取消”
$CancelButton.DialogResult=[System.Windows.Forms.DialogResult]::取消
$objForm.Controls.Add($CancelButton)
#使用此选项,您不需要Add_KeyDown事件在单击OK或Cancel按钮时做出反应
$objForm.AcceptButton=$OKButton
$objForm.CancelButton=$CancelButton
$objLabel=新对象System.Windows.Forms.Label
$objLabel.Location=新对象系统.图纸.尺寸(10,20)
$objLabel.Size=新对象系统.Drawing.Size(280,20)
$objLabel.Text=“请选择”
$objForm.Controls.Add($objLabel)
$objCombobox=新对象System.Windows.Forms.Combobox
$objCombobox.Location=新对象系统.Drawing.Size(10,40)
$objCombobox.Size=新对象系统.Drawing.Size(260,20)
$OBJCOMBOX.Height=70
[void]$objCombobox.Items.Add(“项目1”)
[void]$objCombobox.Items.Add(“第2项”)
[void]$objCombobox.Items.Add(“第3项”)
[void]$objCombobox.Items.Add(“第4项”)
[void]$objCombobox.Items.Add(“第5项”)
$objForm.Controls.Add($objCombobox)
$objForm.Add_显示({$objForm.Activate()})
$dialogResult=$objForm.ShowDialog()
#重要事项:完成后请清理表单
$objForm.Dispose()
如果($dialogResult-ne‘取消’-或$x.Count-eq 0){
#表单已取消或未进行选择
#对于演示,只需写入控制台
写入主机“您已取消对话框或未选择任何内容…”
}
否则{
写主机“您所做的选择是$($x-join',)”-ForegroundColor青色
}
继续我的评论,在修改后的脚本下面
$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
好的,显然现在您希望表单在做出选择并单击
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
}
$x=@()#返回变量(按顺序进行所有选择的数组)
添加类型-AssemblyName System.Windows.Forms
添加类型-AssemblyName System.Drawing
$objForm=新对象System.Windows.Forms.Form
$objForm.Text=“组合框”
$objForm.Size=新对象系统.Drawing.Size(300200)
$objForm.StartPosition=“中心屏幕”
$objForm.KeyPreview=$True
$objForm.Topmost=$True
$OKButton=新建对象System.Windows.Forms.Button
$OKBUTON.Location=新对象系统.图纸.尺寸(75120)
$OKBUTON.Size=新对象系统.Drawing.Size(75,23)
$OKButton.Text=“确定”
$OK按钮。添加\单击({
$selected=$objCombobox.SelectedItem
$script:x+=$objCombobox.SelectedItem
if(![string]::IsNullOrWhiteSpace($selected)){
写入主机“正在执行操作$($已选择)”-ForegroundColor黄色
$objLabel.Text=“正在执行操作$($selected)”
交换机($selected){
“项目1”{}
“项目2”{}
“项目3”{}
“项目4”{}
“项目5”{}
}
$objLabel.Text=“请选择”
$objCombobox.SelectedIndex=-1#将组合框重置为空白
}
})
$objForm.Controls.Add($OKButton)
$CancelButton=新建对象System.Windows.Forms.Button
$CancelButton.Location=新对象系统.图纸.尺寸(150120)
$CancelButton.Size=新对象系统.Drawing.Size(75,23)
$CancelButton.Text=“取消”
$CancelButton.DialogResult=[System.Windows.Forms.DialogResult]::取消
$objForm.Controls.Add($CancelButton)
#使用此选项,您不需要Add_KeyDown事件在单击OK或Cancel按钮时做出反应
$objForm.AcceptButton=$OKButton
$objForm.CancelButton=$CancelButton
$objLabel=新对象System.Windows.Forms.Label
$objLabel.Location=新对象系统.图纸.尺寸(10,20)
$objLabel.Size=新对象系统.Drawing.Size(280,20)
$objLabel.Text=“请选择”
$objForm.Controls.Add($objLabel)
$objCombobox=新对象System.Windows.Forms.Combobox
$objCombobox.Location=新对象系统.Drawin