Listbox Displaymember: odd results

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 1 year and 9 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
cnrops2017
Posts: 2
Last visit: Mon May 22, 2023 9:31 am

Listbox Displaymember: odd results

Post by cnrops2017 »

I'm reading in an array of pscustomobjects from json files.
They appear to be loading into the listbox with $listbox.DataSource
However, the displaymember values never reflect the designated "name" property value. It's always the raw object data

over simplified code:

function:
foreach($file in $files)
{
$item = Get-Content -Path $file.fullname | ConvertFrom-Json
$items += $item
return $items
}

Feeds:
$listbox.DataSource = $items
$listbox.DisplayMember = "name" #I have tried 'name' as well

Here's a screenshot of the object in the debugger:
debugger.jpg
debugger.jpg (72.23 KiB) Viewed 779 times
The listbox populates with the raw data, rather than the name value. Notice the name resolves and loads fine in the 'type name' field
results.jpg
results.jpg (41.24 KiB) Viewed 779 times
It's odd that the name property appears to be lost in the displaymember setting







Product: PowerShell Studio 2022 (64 Bit)
Build: v5.8.207
OS: Windows 10 Pro for Workstations (64 Bit)
Build: v10.0.19044.0
by jvierra » Wed Jun 15, 2022 1:01 am
This is what is needed to convert an array into an iList collection.

$listbox.DataSource = [system.collections.arraylist]$items
Go to full post
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Re: Listbox Displaymember: odd results

Post by Alexander Riedel »

[Topic moved by moderator]
Alexander Riedel
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Listbox Displaymember: odd results

Post by jvierra »

This is what is needed to convert an array into an iList collection.

$listbox.DataSource = [system.collections.arraylist]$items
cnrops2017
Posts: 2
Last visit: Mon May 22, 2023 9:31 am

Re: Listbox Displaymember: odd results

Post by cnrops2017 »

Yesss!!! Thank you!
You know I swear when I first ran this I got a warning about that and I did something like change $items to @($Items) and the error went away. I changed back while goofing around and never saw it again
This topic is 1 year and 9 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