Search code examples
windowsformspowershellcomboboxselecteditemchanged

PowerShell.WindowsForms.Change ListBox via SelectedItemChanged in ComboBox


I went on to develop the script. Now the goal is this: When selecting a department in ListBoxDepartments, the SubDepartments in ListBoxSubDepartments should be displayed. I did it according to the principle mentioned above. But it doesn't work out. Or is there a need for a different approach? Please point to the code how to implement it?

*>>If that is what you want, then you need to also have a SelectedItemChanged event declared on the listbox and when that fires, fill that second listbox with the sub departments. * - I already have SelectedItemChanged on the Deparments ListBox.

Look below in the code:

<#----======= ComboBox Organization =======----#>

$ComboBoxOrganization = New-Object System.Windows.Forms.ComboBox
$ComboBoxOrganization.Sorted = $true
foreach($org in $HashOrganizations.Keys){ 
$ComboBoxOrganization.Items.Add($org)
}
$ComboBoxOrganization.Location  = New-Object System.Drawing.Point(180,30)
$ComboBoxOrganization.SelectedIndex = 0
$ComboBoxOrganization.Add_SelectedIndexChanged({
    $listBoxDepartments.BeginUpdate()
    $listBoxDepartments.Items.Clear()
      foreach ($dept in $HashOrganizations[$this.SelectedItem]) {  
        [void]$listBoxDepartments.Items.Add($dept)
    }
    $listBoxDepartments.EndUpdate()
})
$main_form.Controls.Add($ComboBoxOrganization)

<#----======= ComboBox Organization =======----#>
<#----======= ListBox Departments =======----#>

$listBoxDepartments = New-Object System.Windows.Forms.ListBox
$listBoxDepartments.Location = '180,75'
$listBoxDepartments.Size = '400,100'
$listBoxDepartments.Sorted   = $true
foreach ($dept in $HashOrganizations[$ComboBoxOrganization.SelectedItem]) {  
    [void]$listBoxDepartments.Items.Add($dept)
}
$**listBoxDepartments.Add_SelectedIndexChanged**({
    $listBoxSubDepartments.BeginUpdate()
    $listBoxSubDepartments.Items.Clear()
        foreach($SubDep in $hashCOMPANY1[$this.SelectedItem]) {
            [void]$listBoxSubDepartments.Items.Add($SubDep)
    }
})
$main_form.Controls.Add($listBoxDepartments)


<#----======= ListBox Departments =======----#>
<#----======= ListBox SubDepartments =======----#>

$listBoxSubDepartments = New-Object System.Windows.Forms.ListBox
$listBoxSubDepartments.Location = '180,195'
$listBoxSubDepartments.Size = '400,100'
$listBoxSubDepartments.Sorted   = $true
foreach ($SubDep in $hashCOMPANY1[$listBoxDepartments.SelectedItem]) {  
    [void]$listBoxSubDepartments.Items.Add($SubDep)
}
$main_form.Controls.Add($listBoxSubDepartments)

<#----======= ListBox SubDepartments =======----#>

$main_form.ShowDialog()

If you mean something difference from my code, please show in the code when i should paste this event "SelectedItemChanged"?

Thank you in advance


Solution

  • Too long for explaining in a comment, but what you need is a second Hashtable with the sub departments listed for the various departments.

    So apart from having

    $hashOrganizations = @{
        'COMPANY-1' = 'Department 1','Department 2','Department 3','Department 4'
        'COMPANY-2' = 'Department 5','Department 6','Department 7'
    }
    

    You need a similar Hashtable for the sub departments:

    $hashSubDepartments = @{
        'Department 1' = 'SubDept1-1','SubDept1-2','SubDept1-3','SubDept1-4'
        'Department 2' = 'SubDept2-1','SubDept2-2'
        'Department 3' = 'SubDept3-1','SubDept3-2','SubDept3-3'
        'Department 4' = 'SubDept4-1','SubDept4-2','SubDept4-3'
        # etcetera
    }
    

    Then, as commented you need to add $listBoxSubDepartments.EndUpdate() method in the Add_SelectedIndexChanged event handler so the new content will be shown:

    $listBoxDepartments.Add_SelectedIndexChanged({
        $listBoxSubDepartments.BeginUpdate()
        $listBoxSubDepartments.Items.Clear()
        foreach($SubDep in $hashSubDepartments[$this.SelectedItem]) {
            [void]$listBoxSubDepartments.Items.Add($SubDep)
        }
        $listBoxSubDepartments.EndUpdate()
    })
    

    If need be (when the various companies have equally named departments and/or sub departments, combine the company name with the department name in the $hashSubDepartments keys, like:

    $hashSubDepartments = @{
        'COMPANY-1|Department 1' = 'SubDept1-1','SubDept1-2','SubDept1-3','SubDept1-4'
        'COMPANY-2|Department 1' = 'SubDept1-1','SubDept1-2','SubDept1-3','SubDept1-4'
        'COMPANY-1|Department 2' = 'SubDept2-1','SubDept2-2'
        'COMPANY-2|Department 2' = 'SubDept2-1','SubDept2-2','SubDept1-3'
        'COMPANY-1|Department 3' = 'SubDept3-1','SubDept3-2','SubDept3-3'
        'COMPANY-2|Department 3' = 'SubDept3-1','SubDept3-2'
        'COMPANY-1|Department 4' = 'SubDept4-1','SubDept4-2','SubDept4-3'
        'COMPANY-2|Department 4' = 'SubDept4-1','SubDept4-2','SubDept4-3'
        # etcetera
    }
    

    The event code would then read

    $listBoxDepartments.Add_SelectedIndexChanged({
        $listBoxSubDepartments.BeginUpdate()
        $listBoxSubDepartments.Items.Clear()
        $keyName = '{0}|{1}' -f $ComboBoxOrganization.SelectedItem, $this.SelectedItem
        foreach($SubDep in $hashSubDepartments[$keyName]) {
            [void]$listBoxSubDepartments.Items.Add($SubDep)
        }
        $listBoxSubDepartments.EndUpdate()
    })