Support Forums

Powershell Comboboxes - DisplayMember and ValueMember

Archived support forum for customers who once purchased a PrimalForms product license. This forum is locked.
User avatar
TacticalDragon
Posts: 4
Joined: Sun Mar 03, 2013 12:41 pm

Powershell Comboboxes - DisplayMember and ValueMember

Postby TacticalDragon » Mon Feb 03, 2014 1:24 pm

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
TacticalDragon
Posts: 4
Joined: Sun Mar 03, 2013 12:41 pm

Re: Powershell Comboboxes - DisplayMember and ValueMember

Postby TacticalDragon » Mon Feb 03, 2014 2:06 pm

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: 4354
Joined: Thu Aug 18, 2011 4:56 am

Re: Powershell Comboboxes - DisplayMember and ValueMember

Postby davidc » Mon Feb 03, 2014 2:23 pm

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
TacticalDragon
Posts: 4
Joined: Sun Mar 03, 2013 12:41 pm

Re: Powershell Comboboxes - DisplayMember and ValueMember

Postby TacticalDragon » Mon Feb 03, 2014 2:31 pm

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.

Return to “PrimalForms”

Who is online

Users browsing this forum: No registered users and 1 guest