【问题标题】:Powershell select combobox items trigger using button click not workingPowershell使用按钮单击触发选择组合框项目不起作用
【发布时间】:2018-07-08 22:02:43
【问题描述】:

我目前被困在 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 操作。你确定$Form.ShowDialog() | out-null 是第二个代码而不是第一个代码的一部分吗? ShowDialog() 一般在完整代码的末尾,除非你想在 GUI 关闭后进行一些其他操作。
  • 嗨,这是我的代码。 $button.Add_Click({ if ($ComboBox.SelectedItem -eq 'vpsx') {$IE = new-object -Com "InternetExplorer.Application" $IE.Visible = $true $IE.navigate2("website")} } ) 抱歉,$form.showdiaglog 不是其中的一部分。

标签: wpf powershell powershell-3.0 powershell-4.0


【解决方案1】:

您需要告诉您的 Add_Click 函数它需要做什么。在你的情况下,它可能是这样的:

$button2.AddClick {
  $IE = New-Object -Com 'InternetExplorer.Application'
  $IE.Navigate2("website")
  $IE.Visible = $true
}

如果这不起作用,那么更多细节会很好。你是如何构建 GUI 的?单击按钮时会发生什么?什么不会发生?出现什么错误信息?


编辑:这是一个示例脚本,可以满足您的要求。您应该能够将其复制并粘贴到脚本文件中并运行它,它应该可以工作。告诉我你的进展情况。

[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

【讨论】:

  • 嗨,Darkmoon,我正在通过 XML 构建 gui。单击按钮时没有任何反应。根本没有错误消息 :( 这是我的 xml 代码
  • 你试过上面的代码了吗?结果如何?我在上面问了 4 个问题,你只回答了 1 个,而且你似乎忽略了我建议的代码编辑,尤其是让 IE 可见的部分。如果您查看系统上正在运行的进程,您可能会看到 iexplore.exe 已经在运行,但没有在任何地方显示;如果是这样,那么您需要告诉代码使其可见。
  • 我尝试了上面的代码,不幸的是它没有工作。单击按钮时没有任何反应。完全没有错误。任务管理器中根本没有出现 iexplorer.exe
  • @Veck 我认为您需要为您的问题添加更多上下文。例如,您有一个 $Form.ShowDialog() | out-null 调用 before 似乎将单击处理程序添加到按钮的 switch 语句。 ShowDiaglog() 必须在 switch 语句运行之前返回。
  • 让我们反过来试试这个。我编辑了我的答案以包含一个示例脚本(使用 Windows 窗体而不是 XML 来构建 GUI),该脚本显示了如何挂钩按钮单击事件。让我知道这是否适合您。
【解决方案2】:

我使用组合框属性 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)} })

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-26
    • 1970-01-01
    • 1970-01-01
    • 2021-09-18
    • 1970-01-01
    • 1970-01-01
    • 2015-01-15
    • 2022-01-20
    相关资源
    最近更新 更多