Combobox question

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 7 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
nwills22
Posts: 12
Last visit: Fri Feb 03, 2017 10:16 am

Combobox question

Post by nwills22 »

I want to create a form with 2 comboboxes. the first combobox has 3 choices A, B, C depending on what gets chosen the second combobox items will be populated by a variable $A, $B or $C. How to achieve this?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Combobox question

Post by jvierra »

Populate CB2 on the SelectedItemChanged event.
User avatar
nwills22
Posts: 12
Last visit: Fri Feb 03, 2017 10:16 am

Re: Combobox question

Post by nwills22 »

I tried this but the second combobox does not get populated. Where am I going wrong?

$combobox1_SelectedIndexChanged= {

if ($combobox1.SelectedItem -eq 'A' ) { Load-ComboBox $combobox2.Items.add($A) }
ElseIf ($combobox1.SelectedItem -eq 'B') { Load-ComboBox $combobox2.Items.add($B)}
ElseIf($combobox1.SelectedItem -eq 'C') { Load-ComboBox $combobox2.Items.add($C)}
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Combobox question

Post by jvierra »

Here is the easiest way to reload a second combo.
  1. $combobox1_SelectedIndexChanged = {
  2.     $items = switch ($combobox1.SelectedItem){
  3.         'A'{@('A1', 'A2', 'A2')}
  4.         'B'{@('B1', 'B2', 'B2')}
  5.         'C'{@('C1', 'C2', 'C2')}
  6.     }
  7.     $combobox2.Items.Clear()
  8.     $combobox2.Items.AddRange($items)
  9. }
User avatar
nwills22
Posts: 12
Last visit: Fri Feb 03, 2017 10:16 am

Re: Combobox question

Post by nwills22 »

I tried what you suggested and was able to populate the second combobox but the items listed have some extra characters that I have unsuccessfully tried to remove

For Example:
What I expected to see in the second combo box:

MS Office
Adobe Acrobat
Google Chrome

What gets populated is:

@(Name=MS Office)
@(Name=Adobe Acrobat)
@(Name=Google Chrome)

How do I have it list correctly?
Thanks for your help!


Here is the code:
$comboboxChooseCollType_SelectedIndexChanged= {
$items = switch ($comboboxChooseCollType.SelectedItem)
{
'QA'{ @(Get-CMDeviceCollection | where-object { $PSItem.comment -like "*QA*" } | Select-Object -Property Name) }
'UAC'{ @(Get-CMDeviceCollection | where-object { $PSItem.comment -like "*UAC*" } | Select-Object -Property Name) }
'Production'{ @(Get-CMDeviceCollection | where-object { $PSItem.comment -like "*Production*" } | Select-Object -Property Name) }
}

$comboboxChooseCollection.Items.Clear()
$comboboxChooseCollection.Items.AddRange($items)
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Combobox question

Post by jvierra »

Get-CMDeviceCollection | where-object { $PSItem.comment -like "*QA*" } | Select-Object -Property Name

Should be:

Get-CMDeviceCollection | where-object { $PSItem.comment -like "*QA*" } | Select-Object -Expand Name
User avatar
nwills22
Posts: 12
Last visit: Fri Feb 03, 2017 10:16 am

Re: Combobox question

Post by nwills22 »

That works, Thank you! :D You are gold for newbies like me.
This topic is 7 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked