Combobox question

Ask your PowerShell-related questions, including questions on cmdlet development!
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 2 years and 5 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
gdavies
Posts: 15
Last visit: Fri Oct 08, 2021 11:51 am

Combobox question

Post by gdavies »

I created a form using an old version of Primal Forms CE, it all worked fine until recently when my comboboxes started to display no values.

I decided to move to Powershell Studio and created a test form, I get the same blank combobox, what am I doing wrong?

The code in my original form:

Code: Select all

$handler_form1_Load= 

{
$secgroups1 = Get-ADGroup -Filter 'GroupCategory -eq "Security"' | Select-Object -Expand name | sort-object 
$secgroups1 = ," " + $secgroups1

$editusersecgroupentrybox.DataSource = $secgroups1
}
So the result used to be as son as the form was launched the combobox editusersecgroupentry would list all the security groups in my AD domain, displayed with a blank line at the top to prevent accidental group selection. This was repeated with distribution groups in another combobox and a third one with a list of user objects.
I tried to create a manual list of values to populate the combobox in my test form, again I got blanks so I don't understand where I'm going wrong.

I need to create a new form with multiple comboboxes displaying things like groups, user objects and a couple of manual lists but I seem to have hit a brick wall in my Powershell skills.
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 »

You cannot add a simple collection or simple array to any "DataSource" property on any control. Either convert it to a DataTable or to an "ArrayList".

Remove the line that adds a comma as it serves no purpose.
gdavies
Posts: 15
Last visit: Fri Oct 08, 2021 11:51 am

Re: Combobox question

Post by gdavies »

This did work, and I actually found an old version that it still works on, the code in use is what I posted.

Your comment about the line adding a comma doing nothing, I am taking it you mean this line
$secgroups1 = ," " + $secgroups1

It actually does serve a purpose, it adds a blank line to the top of the drop down list, with this comma added the dropdown list in the combobox has a blank line. This is there to prevent the user accidentally selecting the first group in the list. If the comma is not added the top line of the dropdown is the first group. I added this following issues caused by the group being selected in error. Below are screenshots of the form with and without the comma added
With comma
Image

Without comma
Image

None of this changes the act that my code DID work and still does in an old version of my form, but you're saying this would never work. I don't understand how it worked if it is not possible, in fact the screenshots above show the code working, the dropdown list is populated using the code I posted. The problem is, if I try to edit the form it no longer works so does this mean something has changed within Powershell?
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 »

A ComboBox automatically displays with an empty box if you set the properties correctly.

PowerShell has nothing to do with this unless you are executing the code on different versions of PowerShell. If you are using PS7 then it will work differently because WinForms under PS7 is a completely different codebase as it everything has been converted to the OpenSource version of Net Framework.

Also note that if the AD command returns a singularity, then adding the comma will "stringify" the comma and the singleton and you will just get a string with a comma at the beginning.

Overall, we would never code that the way you have because it has the potential to cause too many problems.

The following is the safest way to dataload a control and it handles singletons.
  1. $secgroups1 = Get-ADGroup -Filter 'GroupCategory -eq "Security"' -Properties Name
  2. $combobox1.DataSource = [System.Collections.ArrayList]$secgroups1
  3. $combobox1.DisplayMember ='Name'
gdavies
Posts: 15
Last visit: Fri Oct 08, 2021 11:51 am

Re: Combobox question

Post by gdavies »

Thank you, the code you provided worked in my new form, I still need to work out how to set the combobox to have a blank line at the top without adding the comma but I'll see what I can find as I work through creating a new form.
Thanks
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 »

Just set the index

$combobox1.SelectedIndex = -1
gdavies
Posts: 15
Last visit: Fri Oct 08, 2021 11:51 am

Re: Combobox question

Post by gdavies »

Thank you, that did the job. I was not aware using -1 would add the blank line.
This topic is 2 years and 5 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