Powershell select 组合框项目使用按钮单击触发无效
Powershell select combobox items trigger using button click not working
我目前卡在 PowerShell 中的特定代码上。我想要完成的是当用户在组合框中选择一个项目并切换按钮时,这将导航到基于用户在组合框中选择的项目的网页或应用程序。
这是我的示例代码。我尝试了两个程序 if 语句和 switch。我这里用的是switch。
这里是 if 语句
if ($ComboBox.SelectedItem -eq "vdi"){
$button2.Add_Click
$ie = new-object -Com "InternetExplorer.Application"
$ie.navigate2("website")
}
这是开关
$Form.ShowDialog() | out-null
$Form.FindName('autool_cmbx')
switch($ComboBox.Text) {
"vdi" {
$button2 = $Form.FindName('go_au')
$button2.Add_Click
$IE= new-object -Com "InternetExplorer.Application"
$IE.navigate2("website")
}
}
您需要告诉您的 Add_Click 函数它需要做什么。在你的情况下,它可能是这样的:
$button2.AddClick {
$IE = New-Object -Com 'InternetExplorer.Application'
$IE.Navigate2("website")
$IE.Visible = $true
}
如果还是不行,再详细点就好了。你是如何构建图形用户界面的?单击按钮时发生了什么?什么没有发生?出现什么错误消息?
编辑: 这是一个示例脚本,它可以满足您的要求。您应该能够将它复制并粘贴到脚本文件中并 运行 它,它应该可以工作。让我知道你过得怎么样。
[reflection.assembly]::LoadWithPartialName("System.Drawing") | Out-Null
[reflection.assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
function Button_OnClick() {
"`$combo.SelectedItem = $($combo.SelectedItem)" | Out-GridView
if ($combo.SelectedItem -eq 'Google') {
Start-Process -FilePath 'C:\Program Files\Internet Explorer\iexplore.exe' -ArgumentList 'http://www.google.com'
} elseif ($combo.SelectedItem -eq 'Microsoft') {
$IE = New-Object -ComObject 'InternetExplorer.Application'
$IE.Navigate2('http://www.microsoft.com')
$IE.Visible = $true
}
}
$combo = New-Object -TypeName System.Windows.Forms.ComboBox
$combo.Location = New-Object -TypeName System.Drawing.Point -ArgumentList 5, 5
$combo.Size = New-Object -TypeName System.Drawing.Point -ArgumentList 100, 25
$combo.Items.Add('Google') | Out-Null
$combo.Items.Add('Microsoft') | Out-Null
$combo.SelectedIndex = 0
$button = New-Object -TypeName System.Windows.Forms.Button
$button.Location = New-Object -TypeName System.Drawing.Point -ArgumentList 5, 35
$button.Size = New-Object -TypeName System.Drawing.Point -ArgumentList 100, 25
$button.Text = 'Launch in IE'
$button.Add_Click({ Button_OnClick })
$form = New-Object -TypeName System.Windows.Forms.Form
$form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle
$form.MaximizeBox = $false
$form.MinimizeBox = $false
$form.Size = New-Object -TypeName System.Drawing.Point -ArgumentList 60, 105
$form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
$form.Controls.Add($combo)
$form.Controls.Add($button)
$form.ShowDialog() | Out-Null
我使用 Selecteditem.content 组合框 属性 让它工作!
$button.Add_Click({
if($ComboBox.SelectedItem.Content -eq "vpsx"){
$IE = new-object -Com "InternetExplorer.Application"
$IE.Visible = $true
$IE.navigate2("https://ctxau001mel0006.global.anz.com/Director/?
locale=en_US#HOME",0x1000)}
})
我目前卡在 PowerShell 中的特定代码上。我想要完成的是当用户在组合框中选择一个项目并切换按钮时,这将导航到基于用户在组合框中选择的项目的网页或应用程序。
这是我的示例代码。我尝试了两个程序 if 语句和 switch。我这里用的是switch。
这里是 if 语句
if ($ComboBox.SelectedItem -eq "vdi"){
$button2.Add_Click
$ie = new-object -Com "InternetExplorer.Application"
$ie.navigate2("website")
}
这是开关
$Form.ShowDialog() | out-null
$Form.FindName('autool_cmbx')
switch($ComboBox.Text) {
"vdi" {
$button2 = $Form.FindName('go_au')
$button2.Add_Click
$IE= new-object -Com "InternetExplorer.Application"
$IE.navigate2("website")
}
}
您需要告诉您的 Add_Click 函数它需要做什么。在你的情况下,它可能是这样的:
$button2.AddClick {
$IE = New-Object -Com 'InternetExplorer.Application'
$IE.Navigate2("website")
$IE.Visible = $true
}
如果还是不行,再详细点就好了。你是如何构建图形用户界面的?单击按钮时发生了什么?什么没有发生?出现什么错误消息?
编辑: 这是一个示例脚本,它可以满足您的要求。您应该能够将它复制并粘贴到脚本文件中并 运行 它,它应该可以工作。让我知道你过得怎么样。
[reflection.assembly]::LoadWithPartialName("System.Drawing") | Out-Null
[reflection.assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
function Button_OnClick() {
"`$combo.SelectedItem = $($combo.SelectedItem)" | Out-GridView
if ($combo.SelectedItem -eq 'Google') {
Start-Process -FilePath 'C:\Program Files\Internet Explorer\iexplore.exe' -ArgumentList 'http://www.google.com'
} elseif ($combo.SelectedItem -eq 'Microsoft') {
$IE = New-Object -ComObject 'InternetExplorer.Application'
$IE.Navigate2('http://www.microsoft.com')
$IE.Visible = $true
}
}
$combo = New-Object -TypeName System.Windows.Forms.ComboBox
$combo.Location = New-Object -TypeName System.Drawing.Point -ArgumentList 5, 5
$combo.Size = New-Object -TypeName System.Drawing.Point -ArgumentList 100, 25
$combo.Items.Add('Google') | Out-Null
$combo.Items.Add('Microsoft') | Out-Null
$combo.SelectedIndex = 0
$button = New-Object -TypeName System.Windows.Forms.Button
$button.Location = New-Object -TypeName System.Drawing.Point -ArgumentList 5, 35
$button.Size = New-Object -TypeName System.Drawing.Point -ArgumentList 100, 25
$button.Text = 'Launch in IE'
$button.Add_Click({ Button_OnClick })
$form = New-Object -TypeName System.Windows.Forms.Form
$form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle
$form.MaximizeBox = $false
$form.MinimizeBox = $false
$form.Size = New-Object -TypeName System.Drawing.Point -ArgumentList 60, 105
$form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
$form.Controls.Add($combo)
$form.Controls.Add($button)
$form.ShowDialog() | Out-Null
我使用 Selecteditem.content 组合框 属性 让它工作!
$button.Add_Click({
if($ComboBox.SelectedItem.Content -eq "vpsx"){
$IE = new-object -Com "InternetExplorer.Application"
$IE.Visible = $true
$IE.navigate2("https://ctxau001mel0006.global.anz.com/Director/?
locale=en_US#HOME",0x1000)} })