Page 1 of 1

Powershell Comboboxes - DisplayMember and ValueMember

Posted: Mon Feb 03, 2014 12:24 pm
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,

Re: Powershell Comboboxes - DisplayMember and ValueMember

Posted: Mon Feb 03, 2014 1:06 pm
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"


Re: Powershell Comboboxes - DisplayMember and ValueMember

Posted: Mon Feb 03, 2014 1:23 pm
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

Re: Powershell Comboboxes - DisplayMember and ValueMember

Posted: Mon Feb 03, 2014 1:31 pm
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.