1
votes

I'm creating a powershell GUI with two comboboxes. The first one contains Countries, the second one should be populated with cities based on the country that has been selected in the first combobox

e.g when "USA" is selected in combobox1, "New York", "Detroit" and "Seattle" should show up in combobox2, when "England" is selected in combobox1, "London" and "Essex" should show up in combobox2 and so on

Here's a very shortened version of my script:

$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()

I added a simple IF statement for one country fo the sake of this example but not even that is working. I've been researching for days how to accomplish this but couldn't find anything or wasn't able to fully understand some solutions. Is there some simple way to do this?

Any help is appreciated!

1
The form will only update during an event - it doesn't sit there checking your "IF" statement to see if anything's changed. Add an event for the first combobox being changed and put your code into that event to populate the second combobox.Scepticalist

1 Answers

1
votes

See my comment above, something like this:

$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()