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