Dynamic ComboBox not loading

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 5 years and 10 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
coryjiggens
Posts: 8
Last visit: Thu Sep 07, 2023 7:16 am

Dynamic ComboBox not loading

Post by coryjiggens »

To help you better we need some information from you.

*** Please fill in the fields below. If you leave fields empty or specify 'latest' rather than the actual version your answer will be delayed as we will be forced to ask you for this information. ***

Product, version and build: PowerShell Studio 2018 v5.5.150
32 or 64 bit version of product: 64bit
Operating system:
32 or 64 bit OS: 64 Bit


I am trying to create a combo box dynamically with following code. When i run it, It doesn't populate my combo box.
  1. $combobox1_SelectedIndexChanged={
  2.     #TODO: Place custom script here
  3.     $Region = (Get-AWSRegion).Name
  4.     Update-ComboBox -ComboBox $combobox1 -Items $Region
  5. }
Is there a setting in properties i need to set to display in the combo box.



I


DO NOT POST SUBSCRIPTIONS, KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Dynamic ComboBox not loading

Post by davidc »

[TOPIC MOVED TO POWERSHELL GUIS FORUM BY MODERATOR]
David
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Dynamic ComboBox not loading

Post by jvierra »

To reload a ComboBox with new items this is the easiest way.

Code: Select all

$combobox1_SelectedIndexChanged={
    $items = (Get-AWSRegion).Name
    $combobox1.Items.Clear()
    $combobox1.Items.AddRange($items)
}
User avatar
coryjiggens
Posts: 8
Last visit: Thu Sep 07, 2023 7:16 am

Re: Dynamic ComboBox not loading

Post by coryjiggens »

That still didn't populate my combo box. Any other ideas why it's not working?
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

Re: Dynamic ComboBox not loading

Post by mxtrinidad »

Make sure to populate the ComboBox control, the code need to be on the button to create the array and not in the "$combobox1_SelectedIndexChanged" event. Then, you'll see the list when you click in the ComboBox.

:)

Code: Select all

$button2_Click={
	#TODO: Place custom script here
	$items = (dir).Name
	$combobox1.Items.Clear()
	$combobox1.Items.AddRange($items)
}

$combobox1_SelectedIndexChanged = {
	#TODO: Place custom script here
	$labelDisplayComboSelected.Text = $combobox1.SelectedItem.ToString();
	
}

jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Dynamic ComboBox not loading

Post by jvierra »

coryjiggens wrote: Mon May 14, 2018 11:48 am That still didn't populate my combo box. Any other ideas why it's not working?
Works fine for me. Are you sure the event is actually being called? Place a breakpoint in the event an be sure it is being caught.

Are you trying to use this event for the initial load of the box? The event will never be called if the ComboBox is empty. Use the "Load" event to load controls.
User avatar
coryjiggens
Posts: 8
Last visit: Thu Sep 07, 2023 7:16 am

Re: Dynamic ComboBox not loading

Post by coryjiggens »

Could I do it on load form event instead? If so how would I do that?
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

Re: Dynamic ComboBox not loading

Post by mxtrinidad »

Make sure to check you got data in your $item variable before populating the ComboBox. If you're using a Cloud cmdlet and you're not connecting then the variable is null (not data).
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Dynamic ComboBox not loading

Post by jvierra »

coryjiggens wrote: Mon May 14, 2018 12:31 pm Could I do it on load form event instead? If so how would I do that?
You can load the box on any event that is actually fired. Use the forms "Load" event, a button event or an other event that is fired by any control. The event must fire. An empty ComboBox will never fire a "SelectedIndexChanged" event.
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

Re: Dynamic ComboBox not loading

Post by mxtrinidad »

As JVierra stated, you can't execute the code to populate the ComboBox in the "SelectedIndexChanged" event.
See the sample code I provided, it shows it work.

:)
This topic is 5 years and 10 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