PowerShell.WindowsForms.Change ListBox. Remove duplicate data

62 views Asked by At

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. 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 you change the double data?

$hash  = @{
        
        'COMPANY-4' = 'Department 1','Department 2','Department 3'
        'COMPANY-3' = 'Department 4','Department 5','Department 6'
        'COMPANY-2' = 'Department 7','Department 8','Department 9','Department 10'
        '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'

}

$hashCOMPANY2 = @{
    
        'Department 7' = 'SubDep-1','SubDep-2','SubDep-3','SubDep-4'
        'Department 8' = 'SubDep-5','SubDep-6','SubDep-7','SubDep-8'
        'Department 9' = 'SubDep-9','SubDep-10','SubDep-11','SubDep-12'
        'Department 10' = 'SubDep-9','SubDep-13','SubDep-14','SubDep-15'

}

.....

$ComboBoxOrganization = New-Object System.Windows.Forms.ComboBox
$ComboBoxOrganization.Sorted = $true
foreach($org in $hash.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 $hash[$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
$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)


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

$listBoxSubDepartments = New-Object System.Windows.Forms.ListBox
$listBoxSubDepartments.Location = '180,195'
$listBoxSubDepartments.Size = '400,100'
$listBoxSubDepartments.Sorted   = $true

$main_form.Controls.Add($listBoxSubDepartments)

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

$main_form.ShowDialog()

enter image description here enter image description here

0

There are 0 answers