根据对另一个组合框-Powershell

时间:2019-08-30 12:53:20

标签: powershell combobox

我正在创建带有两个组合框的Powershell GUI。 第一个包含国家/地区,第二个应该根据第一个组合框中选择的国家/地区填充城市

例如,当在组合框1中选择“美国”时,在组合框2中应显示“纽约”,“底特律”和“西雅图”,而在组合框1中选择了“英国”时,则应显示“伦敦”和“埃塞克斯”在combobox2中,依此类推

这是我的脚本的简短版本:

$Countries=@("USA","England","Canada")

$CitiesUS=@("New York","Detroit","Seattle")
$CitiesEN=@("London","Essex")
$CitiesCA=@("Toronto","Vancouver")

$Form                                  =      New-Object System.Windows.Forms.Form
$Form.Size                             =      New-Object System.Drawing.Size(300,300)  
$Combobox1                             =      New-Object System.Windows.Forms.Combobox
$Combobox1.Location                    =      New-Object System.Drawing.Size(26,50)  
$Combobox1.Size                        =      New-Object System.Drawing.Size(105,20)
$Combobox1.items.AddRange($Countries)
$combobox2                             =      New-Object System.Windows.Forms.Combobox
$combobox2.Location                    =      New-Object System.Drawing.Size(143,50)  
$combobox2.Size                        =      New-Object System.Drawing.Size(105,20)
$Form.Controls.Add($combobox1)
$Form.Controls.Add($combobox2)

## combobox function

if     ($combobox1.SelectedItem -eq ("USA")) {
       $combobox2.Items.Clear()
       foreach ($City in $CitiesUS) {
       $combobox2.Items.Add($City)}
       }

$Form.ShowDialog()

为这个示例,我为一个国家/地区添加了一个简单的IF语句,但即使这样也没有用。 我一直在研究如何实现此目的的几天,但找不到任何东西或无法完全理解某些解决方案。有一些简单的方法可以做到这一点吗?

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

请参阅上面的我的评论,如下所示:

$Countries=@("USA","England","Canada")

$CitiesUS=@("New York","Detroit","Seattle")
$CitiesEN=@("London","Essex")
$CitiesCA=@("Toronto","Vancouver")

$Form                                  =      New-Object System.Windows.Forms.Form
$Form.Size                             =      New-Object System.Drawing.Size(300,300)  
$Combobox1                             =      New-Object System.Windows.Forms.Combobox
$Combobox1.Location                    =      New-Object System.Drawing.Size(26,50)  
$Combobox1.Size                        =      New-Object System.Drawing.Size(105,20)
$Combobox1.items.AddRange($Countries)
$combobox2                             =      New-Object System.Windows.Forms.Combobox
$combobox2.Location                    =      New-Object System.Drawing.Size(143,50)  
$combobox2.Size                        =      New-Object System.Drawing.Size(105,20)
$Form.Controls.Add($combobox1)
$Form.Controls.Add($combobox2)
# Populate Combobox 2 When Combobox 1 changes
$ComboBox1_SelectedIndexChanged= {
    $combobox2.Items.Clear() # Clear the list
    $combobox2.Text = $null  # Clear the current entry
    Switch ($ComboBox1.Text) {
        "USA" {        
            $CitiesUS | ForEach { $combobox2.Items.Add($_) }
        }
        "England" {
            $CitiesEN | ForEach { $combobox2.Items.Add($_) }
        }
        "Canada" {
            $CitiesCA | ForEach { $combobox2.Items.Add($_) }
        }
    }
}

$ComboBox1.add_SelectedIndexChanged($ComboBox1_SelectedIndexChanged)

$Form.ShowDialog()