Combobox returns Null

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 8 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
i-worx.admin
Posts: 6
Last visit: Wed Feb 07, 2024 12:04 pm

Combobox returns Null

Post by i-worx.admin »

Hi

I am trying to use the selected item within a combobox to store it in a variable.

this variable is then going to be used in another command for sorting a SQL Query.

I have tested to make sure that the issue is not the SQL Query, as manually entering the value, as opposed to using a variable works just fine and the query itself already uses other variables with no issue.

This is the SQL query in question:
"Select * FROM Database.dbo.table order by $variable;"

It appears that for whatever reason the combobox is returning a null output.

I have tested this by using write-host $variable (the variable used to store the selecteditem in the combobox)

I have set the combobox to use a collection of items and the style is set to dropdownlist.

I have tried passing using the .text, .selecteditem, .selectedvalue, and .selectedIndex.

The first three return nothing and the last returns -1.

Also as a final note, how do I make it so the combobox auto selects the first item.

Thank you for your help.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Combobox returns Null

Post by jvierra »

Just reference the combobox directly.

"Select * FROM Database.dbo.table order by $($combobox1.SelectedItem)"
User avatar
i-worx.admin
Posts: 6
Last visit: Wed Feb 07, 2024 12:04 pm

Re: Combobox returns Null

Post by i-worx.admin »

jvierra wrote:Just reference the combobox directly.

"Select * FROM Database.dbo.table order by $($combobox1.SelectedItem)"

That worked!!. I managed to get it to work another way as well, by creating functions. I think the issue I am having is mainly do to the fact that when my form loads, the combobox is blank.

Is there a way to have it load with the top value selected?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Combobox returns Null

Post by jvierra »

$combobox1.SelectedIndex = 1
User avatar
i-worx.admin
Posts: 6
Last visit: Wed Feb 07, 2024 12:04 pm

Re: Combobox returns Null

Post by i-worx.admin »

Sadly that did not work.

Here is what I receive when I input that in to the script

ERROR: Exception setting "SelectedIndex": "InvalidArgument=Value of '1' is not valid for 'SelectedIndex'.
ERROR: Parameter name: SelectedIndex"
MainForm.psf (28, 1): ERROR: At Line: 28 char: 1
ERROR: + $combobox1.SelectedIndex = 1
ERROR: + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR: + CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
ERROR: + FullyQualifiedErrorId : ExceptionWhenSetting
ERROR:
WARNING: Incorrect syntax near ';'.
WARNING: Collection cannot be null.
Parameter name: 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 returns Null

Post by jvierra »

You cannot set the index before you load the combobox.
This topic is 8 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