0

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

  • I'm seeing lots of errors. First you need to add at beginning of code "using assembly System.Windows.Forms" Second the variable $HashOrganizations is empty which creates a lot of errors. Are you seeing any error when you run the code? – jdweng Mar 06 '23 at 08:07
  • Oh! Yes! System.Windows.Forms - already added, i just not put in this. And Variable $HashOrganizations to anounced in all code and have a values – Catcher Rem Mar 06 '23 at 08:49
  • I just put this a part of code: $HashOrganizations = @{ 'COMPANY-4' = 'Department 1','Department 2','Department 3' 'COMPANY-3' = 'Department 4','Department 5','Department 6' 'COMPANY-2' = 'Department 7','Department 8','Department 9' 'COMPANY-1' = 'Department 10','Department 11','Department 12' } $hashCOMPANY1 = @{ 'Department 10' = 'SubDep-1','SubDep-2','SubDep-3','SubDep-4' 'Department 11' = 'SubDep-5','SubDep-6','SubDep-7','SubDep-8' 'Department 12' = 'SubDep-9','SubDep-10','SubDep-11','SubDep-12' } – Catcher Rem Mar 06 '23 at 08:50
  • In this part of the code, i have some error: Indexing operation failed, array index is calculated as NULL. line:149 character:21 + ... oreach ($SubDep in $hashCOMPANY1[$listBoxDepartments.SelectedItem]) { + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : NullArrayIndex – Catcher Rem Mar 06 '23 at 08:53
  • You $hashOrganization definitions has errors. You need to fix. – jdweng Mar 06 '23 at 10:31
  • I don't understand, what the error has $hashOrganization - can You show me this error? – Catcher Rem Mar 06 '23 at 10:52
  • After running code just type in PS : $HashOrganizations – jdweng Mar 06 '23 at 11:01
  • You forgot to put `$listBoxSubDepartmentsEndUpdate()` as last line of the `SelectedIndexChanged()` event handler on the $listBoxDepartments – Theo Mar 06 '23 at 13:44

1 Answers1

0

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()
})
Theo
  • 57,719
  • 8
  • 24
  • 41
  • Thank you. It's working. In my code just missed this variable $listBoxSubDepartments.EndUpdate() in code block $listBoxDepartments.Add_SelectedIndexChanged({...}) – Catcher Rem Mar 06 '23 at 14:22
  • @CatcherRem Yep, thought as much ;). You can do without `BeginUpdate()` and `EndUpdate()`, but then the screen will try to refresh on each addition which becomes slow and tiresome for the user – Theo Mar 06 '23 at 14:25
  • It works in principle. There is a small problem, there is a double data. For example. The companies COMPANY1 and COMPANY2 have an Accounting Department (in both companies it is called the same) But they have different sub-departments. With this code logic, it turns out that all subdepartments from the accounting departments of the two companies fall into the listboxSubDepartments. – Catcher Rem Mar 07 '23 at 08:42
  • ...Example: Accounting COMPANY1 has subdepartments Subdepartment1 and Subdepartment2. Accounting COMPANY2 has subdepartments Subdepartment1 and Subdepartment3. By selecting COMPANY1 or COMPANY2 and Department Accounting in listboxsubdepartment I get the data: Subdepartment1, Subdepartment1, Subdepartment2, subdepartment3. How can change the double data? – Catcher Rem Mar 07 '23 at 08:42
  • Part of code: $listBoxDepartments = New-Object System.Windows.Forms.ListBox $listBoxDepartments.Location = '180,75' $listBoxDepartments.Size = '400,100' $listBoxDepartments.Sorted = $true foreach ($dept in $hash[$ComboBoxOrganization.SelectedItem]) { [void]$listBoxDepartments.Items.Add($dept) } – Catcher Rem Mar 07 '23 at 08:50
  • ... $listBoxDepartments.Add_SelectedIndexChanged({ $listBoxSubDepartments.BeginUpdate() $listBoxSubDepartments.Items.Clear() foreach($SubDep in $hashCOMPANY1[$this.SelectedItem]) { [void]$listBoxSubDepartments.Items.Add($SubDep) } foreach($SubDep in $hashCOMPANY2[$this.SelectedItem]) { [void]$listBoxSubDepartments.Items.Add($SubDep) } $listBoxSubDepartments.EndUpdate() }) $main_form.Controls.Add($listBoxDepartments) – Catcher Rem Mar 07 '23 at 08:51
  • @CatcherRem I also gave you code how to combine the company name with the main department so you can store the subdepartments specific for each organization inside one Hashtable. I would use that instead of hashes-per-company – Theo Mar 07 '23 at 19:52