Search code examples
powershellcombobox

Populate ComboBox2 depending on ComboBox1 selection


Few days of searching and trying multiple options, but nothing is working actually. With the previous code posted.

I've imported all my objects from XAML (all set in variables), I don't know if that would be a problem for that. I don't think since everything else seems to work properly.

I just want my Tab2CBB2 to show values depending on the selection of Tab1CBB1. Anyone could help ? (I haven't paste the entire code, neither the paths but you can probably help me with that). Note that those are two of my multiple tries. Thanks

[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')

[xml]$xaml = @" ...

"@

#Read XAML (Parse it)
$reader=(New-Object System.Xml.XmlNodeReader $XAML)
$Window=[Windows.Markup.XamlReader]::Load( $reader )

$listQA14 = Get-ChildItem $pathQA14 -name
$listQA15 = Get-ChildItem $pathQA15 -name
$listDBQA = Get-ChildItem $pathDBQA -name

$Tab1CBB1_SelectedIndexChanged= {
    $Tab1CBB2.Items.Clear() # Clear the list
    $Tab1CBB2.Text = $null  # Clear the current entry
    Switch ($Tab1CBB1.Text) {
        'QA14' {        
            $ListQA14 | ForEach { $Tab1CBB2.Items.Add($_) }
        }


        'QA15' {        
            $ListQA15 | ForEach { $Tab1CBB2.Items.Add($_) }
        }

        'ARIELDBQA' {        
            $ListDBQA | ForEach { $Tab1CBB2.Items.Add($_) }
        }
        }
        }

$Tab1CBB1.add_SelectedIndexChanged($Tab1CBB1_SelectedIndexChanged)


#Displays the Window
$Window.ShowDialog() | out-null

Here is another option tried :

#ComboBox1------
$ItemsCBB1 = @('ARIELDBQA','QA14','QA15')
foreach ($Item in $ItemsCBB1) {
$Tab1CBB1.Items.Add($Item)
}


#ComboBox2-------

    if ($Tab1CBB1.SelectedItem -eq 'QA14') {
        $Tab1CBB2.Items.Clear()
        foreach ($Serie in $listQA14) {
        $Tab1CBB2.Items.Add($Serie)
        }
    }
     

    elseif ($Tab1CBB1.SelectedItem -eq 'QA15') {
        $Tab1CBB2.Items.Clear()
        foreach ($Serie in $listQA15) {
        $Tab1CBB2.Items.Add($Serie)
           }
    }

    elseif ($Tab1CBB1.SelectedItem -eq 'listDBQA') {
           $Tab1CBB2.Items.Clear()
           foreach ($Serie in $listDBQA) {
           $Tab1CBB2.Items.Add($Serie)
           }
    }

Tried this also : $Selection = $Tab1CBB1.SelectedItem.ToString() Variable $selection put after 'if', but not working Note that when I indicate what the current selection would be, it is working properly. The problem seems to come from 'recording' the selection a the time of clicking... Thnaks !


Solution

  • Basically, all you have to do is inside the $Tab1CBB1_SelectedIndexChanged scriptblock use the various lists with script scoping.

    Without that, the variables are unknown inside the script block.

    $Tab1CBB1_SelectedIndexChanged = {
        $Tab1CBB2.Items.Clear() # Clear the list
        $Tab1CBB2.Text = $null  # Clear the current entry
        switch ($Tab1CBB1.Text) {
            'QA14'      { $script:ListQA14 | ForEach-Object { $Tab1CBB2.Items.Add($_) } }
            'QA15'      { $script:ListQA15 | ForEach-Object { $Tab1CBB2.Items.Add($_) } }
            'ARIELDBQA' { $script:ListDBQA | ForEach-Object { $Tab1CBB2.Items.Add($_) } }
        }
    }
    

    Another method could be to dynamically get the items to enter in the combobox, especially since these are file lists and can change while your form is being used:

    $Tab1CBB1_SelectedIndexChanged = {
        $Tab1CBB2.Items.Clear() # Clear the list
        $Tab1CBB2.Text = $null  # Clear the current entry
        switch ($Tab1CBB1.Text) {
            'QA14'      { $path = $script:pathQA14 ; break }
            'QA15'      { $path = $script:pathQA15 ; break }
            'ARIELDBQA' { $path = $script:pathDBQA }
        }
        Get-ChildItem -Path $path -Name | ForEach-Object { $Tab1CBB2.Items.Add($_) }
    }