ListBox issue

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 8 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
User avatar
ltwillis
Posts: 6
Last visit: Thu Mar 04, 2021 1:08 pm

ListBox issue

Post by ltwillis »

I have the PS script below:
PowerShell Code
Double-click the code block to select all.
$holdval2 = Get-Mailbox $MailboxAddress.Text
	$WhenMailboxCreated.Text = $holdval2.WhenMailboxCreated
	$ADUserObjectCreated.Text = $holdval2.WhenCreated
	
	$EA = $holdval2.EmailAddresses
	Load-ListBox $Proxys $EA
$EA ends up holding a list of email addresses (6 of them) in an array. But the only thing that displays in the ListBox is "(Collection)", which is obviously not what is wanted here. I would like for the 6 email addresses to show up in the ListBox, one per line.

What am I missing here?
User avatar
SAPIEN Support Forums
Posts: 945
Last visit: Thu Oct 22, 2015 1:10 pm

ListBox issue

Post by SAPIEN Support Forums »

This is an automated post. A real person will respond soon.

Thank you for posting, ltwillis.

Here are some hints to help you get an accurate and complete answer to your question.

Ask in the best forum: If you asked in the wrong forum, just copy your question to the right forum.

Anticipate follow-up questions!

Did you remember to include the following?
  • 1. Product, version and build
    2. 32 or 64 bit product
    3. Operating system, e.g. Windows 7 64 bit.
    4. Attach a screenshot, if applicable
    5. Attach logs, crash reports, etc., in a ZIP file
If not, please take a moment to edit your original post or reply to this one.

*** Make sure you do not post any licensing information ***
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: ListBox issue

Post by jvierra »

Use this:

$proxys.Items.AddRange($EA)
User avatar
ltwillis
Posts: 6
Last visit: Thu Mar 04, 2021 1:08 pm

Re: ListBox issue

Post by ltwillis »

I didn't see your post/reply until today. I found the issue though. Turns out just had to make one simple change to line 5 as below:

[System.Array]$EA = $holdval2.EmailAddresses

For some reason, the data type for $EA was being set to ArrayList instead of just Array, and the Load-Listbox function is expecting an object of data type [array]. So once I added the "[System.Array]" right before $EA it all worked perfectly.

By the way, do you know exactly what the difference is between the two data types Array and ArrayList??? and why are there two?

Thanks again.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: ListBox issue

Post by jvierra »

ArrayList is correct however you had only one item in the list so it failed. Forcing it to be an array allowed the function to create an arraylist.

This is what is happening:
PowerShell Code
Double-click the code block to select all.
PS C:\scripts> [collections.arraylist]'xxx'
Cannot convert the "xxx" value of type "System.String" to type "System.Collections.ArrayList".
At line:1 char:1
+ [collections.arraylist]'xxx'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : ConvertToFinalInvalidCastException

PS C:\scripts> [collections.arraylist][array]'xxx'
xxx
PS C:\scripts>
If the object passed is not an array then the cast fails. Forcing the string into an array fixes this.

A simple way is @('some string')
This topic is 8 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