Populate ComboBox2 depending on ComboBox1 selection

喜欢而已 提交于 2020-12-15 06:17:57

问题


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 !


回答1:


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($_) }
}



回答2:


A simple example of what you seem to be after is something like this.

### Powershell populate ComboBox 2 based on ComboBox 1 Selection
Add-type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

$form1 = New-Object 'System.Windows.Forms.Form'

$labelDoubleClickOnAnyItem = New-Object 'System.Windows.Forms.Label'

$listbox2 = New-Object 'System.Windows.Forms.ListBox'
$listbox1 = New-Object 'System.Windows.Forms.ListBox'

$buttonOK = New-Object 'System.Windows.Forms.Button'

$form1_Load={
    $items=1..9 | 
    ForEach-Object {"List item $_"}
    
    $listbox1.Items.AddRange($items)
}

$listbox1_MouseDoubleClick=[System.Windows.Forms.MouseEventHandler]{
    $listbox2.Items.Add($listbox1.SelectedItem)
    $listbox1.Items.Remove($listbox1.SelectedItem)
}

$listbox2_MouseDoubleClick=[System.Windows.Forms.MouseEventHandler]{
    $listbox1.Items.Add($listbox2.SelectedItem)
    $listbox2.Items.Remove($listbox2.SelectedItem)
}

$form1.Controls.Add($labelDoubleClickOnAnyItem)
$form1.Controls.Add($listbox2)
$form1.Controls.Add($listbox1)
$form1.Controls.Add($buttonOK)
$form1.AcceptButton = $buttonOK
$form1.ClientSize = '378, 388'
$form1.FormBorderStyle = 'FixedDialog'
$form1.MaximizeBox = $False
$form1.MinimizeBox = $False
$form1.Name = 'form1'
$form1.StartPosition = 'CenterScreen'
$form1.Text = 'Form'
$form1.add_Load($form1_Load)

$labelDoubleClickOnAnyItem.Font = 'Microsoft Sans Serif, 9.75pt, style=Bold, Italic'
$labelDoubleClickOnAnyItem.Location = '13, 13'
$labelDoubleClickOnAnyItem.Name = 'labelDoubleClickOnAnyItem'
$labelDoubleClickOnAnyItem.Size = '353, 41'
$labelDoubleClickOnAnyItem.TabIndex = 3
$labelDoubleClickOnAnyItem.Text = 'Double click on any item to move it from one box to the other.'

$listbox2.FormattingEnabled = $True
$listbox2.Location = '200, 67'
$listbox2.Name = 'listbox2'
$listbox2.Size = '166, 277'
$listbox2.Sorted = $True
$listbox2.TabIndex = 2
$listbox2.add_MouseDoubleClick($listbox2_MouseDoubleClick)

$listbox1.FormattingEnabled = $True
$listbox1.Location = '12, 67'
$listbox1.Name = 'listbox1'
$listbox1.Size = '167, 277'
$listbox1.Sorted = $True
$listbox1.TabIndex = 1
$listbox1.add_MouseDoubleClick($listbox1_MouseDoubleClick)

$buttonOK.Anchor = 'Bottom, Right'
$buttonOK.DialogResult = 'OK'
$buttonOK.Location = '291, 353'
$buttonOK.Name = 'buttonOK'
$buttonOK.Size = '75, 23'
$buttonOK.TabIndex = 0
$buttonOK.Text = '&OK'

$form1.ShowDialog()

Or even this using just the ComboBox elements

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

$Form                            = New-Object system.Windows.Forms.Form
$Form.ClientSize                 = New-Object System.Drawing.Point(498,459)
$Form.text                       = 'Form'
$Form.TopMost                    = $false
$Form.StartPosition              = 'CenterScreen'

$ComboBox1                       = New-Object system.Windows.Forms.ComboBox
$ComboBox1.text                  = ''
$ComboBox1.width                 = 216
$ComboBox1.height                = 75
@('ComboxItem1','ComboxItem2','ComboxItem3') | 
ForEach-Object {[void] $ComboBox1.Items.Add($_)}
$ComboBox1.location              = New-Object System.Drawing.Point(31,41)
$ComboBox1.Font                  = New-Object System.Drawing.Font('Microsoft Sans Serif',10)

$ComboBox2                       = New-Object system.Windows.Forms.ComboBox
$ComboBox2.text                  = ''
$ComboBox2.width                 = 213
$ComboBox2.height                = 38
$ComboBox2.location              = New-Object System.Drawing.Point(32,73)
$ComboBox2.Font                  = New-Object System.Drawing.Font('Microsoft Sans Serif',10)

Function Add-ItemToComboBox2
{
    $ComboBox2.Items.Add($ComboBox1.SelectedItem)
    $ComboBox2.Refresh()
}

$Form.controls.AddRange(
    @(
        $ComboBox1,
        $ComboBox2
    )
)

$ComboBox1.Add_SelectedIndexChanged({ Add-ItemToComboBox2 })

[void]$Form.ShowDialog()

You have not stated what UX/UI tool you are using to design and implement your GUI. Yet, hard coding is also just a challenge. So consider using one of these for your UX/UI effort. Your UX/UI should just work, whether you have PowerShell code behind it (or any other language). Your PowerShell code should just work, regardless of the UX/UI that may be implemented. Your UX/UI code should be separate from your ops code. You call your ops code from the UX/UI.

Free:

https://poshgui.com

https://www.youtube.com/results?search_query=poshgui

https://visualstudio.microsoft.com/vs/community

Be sure to thoroughly read the licensing agreement for VS.

https://www.youtube.com/results?search_query=vs+2019+community+winforms

Purchase

https://ironmansoftware.com/psscriptpad

https://www.youtube.com/results?search_query=psscriptpad

https://ironmansoftware.com/powershell-pro-tools

https://www.youtube.com/results?search_query=powershell-pro-tools

https://www.sapien.com/software/powershell_studio

https://www.youtube.com/results?search_query=Sapien%27s+powershell+studio++winforms

https://visualstudio.microsoft.com/vs

https://www.youtube.com/results?search_query=vs+2019+community+winforms



来源:https://stackoverflow.com/questions/64849654/populate-combobox2-depending-on-combobox1-selection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!