Powershell Comboboxes - DisplayMember and ValueMember

Archived support forum for customers who once purchased a PrimalForms product license. This forum is locked.
This topic is 10 years and 1 month 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.
User avatar
DarcyBednas
Posts: 4
Last visit: Thu Feb 11, 2021 1:02 pm

Powershell Comboboxes - DisplayMember and ValueMember

Post by DarcyBednas »

Hello,

I have been struggling to find out if powershell will allow comboboxes to behave like the .net version of comboboxes.

Within .net if you create a combobox, you are able to add a field to the combobox that shows text in the drop down. You can also specify the value for each item you see in that drop down.If i choose World War I from the drop down than i am expecting to be able to grab the value WWI and use it.

For Example:

Display Member:
World War I
World War II
World War III

Value Member:
WWI
WWII
WWIII


I've seen posts here about using the Load-Combobox function but that function doesnt seem to let you set the Value member. Has anyone else run into this? Currently i have a work around to create an array that holds the value member and than based on the selected index for the display member i just choose the same index in my array that is holding the value member. Very cludgy.

Please let me know what you think. Maybe im just going crazy. :geek:

Cheers,
User avatar
DarcyBednas
Posts: 4
Last visit: Thu Feb 11, 2021 1:02 pm

Re: Powershell Comboboxes - DisplayMember and ValueMember

Post by DarcyBednas »

Below is the code i've been using, the Load-ComboBox i did not write, but have tweaked it to be able to add a ValueMember with no luck. once the combobox is loaded the value member for every single item in the drop down becomes "description" when it should be grabbing the description attribute from Active Directory. Hopefully this helps.

Code: Select all

Load-ComboBox $CboDistrictName (Get-ADOrganizationalUnit -Filter '(name -like "*")' -SearchScope OneLevel -SearchBase "OU=Districts,DC=WorldTrade" -ResultPageSize 1000 -properties * | Select Name, description) -DisplayMember name -ValueMember description



function Load-ComboBox 
{
<#
.SYNOPSIS
    This functions helps you load items into a ComboBox.

.DESCRIPTION
    Use this function to dynamically load items into the ComboBox control.

.PARAMETER  ComboBox
    The ComboBox control you want to add items to.

.PARAMETER  Items
    The object or objects you wish to load into the ComboBox's Items collection.

.PARAMETER  DisplayMember
    Indicates the property to display for the items in this control.
    
.PARAMETER  Append
    Adds the item(s) to the ComboBox without clearing the Items collection.
#>
    
Param 
(
    [Parameter(Mandatory=$true)]
    [System.Windows.Forms.ComboBox]$ComboBox,
    [Parameter(Mandatory=$true)]$Items,
    [Parameter(Mandatory=$false)]
    [string]$DisplayMember,
    [string]$ValueMember,
    [switch]$Append
)

if(-not $Append)
{
    $comboBox.Items.Clear()    
}
    
if($Items -is [Array])
{
    $comboBox.Items.AddRange($Items)
}
else
{
    $comboBox.Items.Add($Items)  
}

$comboBox.ValueMember = "description"
$comboBox.DisplayMember = "name"

User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Powershell Comboboxes - DisplayMember and ValueMember

Post by davidc »

The ValueMember only works if you are data binding and using the ComboBox's DataSource property.

You could just access the property from the selected object:
PowerShell Code
Double-click the code block to select all.
$combobox1.SelectedItem.Description
David
David
SAPIEN Technologies, Inc.
User avatar
DarcyBednas
Posts: 4
Last visit: Thu Feb 11, 2021 1:02 pm

Re: Powershell Comboboxes - DisplayMember and ValueMember

Post by DarcyBednas »

Hello David,

Thank you for your help. I just tried that and it worked :D. I wonder why they only made Value member work with a datasource. I dont feel crazy anymore.
This topic is 10 years and 1 month 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.