output to list of size 1

I am running a form to allow the end user to select 1 or more files from a listing. I am using a System.Windows.Forms.CheckedListBox. In my function I define an array as such:
[System.Collections.ArrayList]$outList = @()

When the subform with the scheckedlistbox is closed (via an OK button) I r run this:

    $okButton.Add_Click(
        {
        $objListBox.CheckedItems
        foreach ($anItem in $objListBox.CheckedItems)
            {
            $outList.Add($anItem)
            }
        $subForm.Close()
        })

at the end of the function that calls the form, I call"
return $outList

My delema is that even though I define $outList as an array, if there is only one item in it, I get the error:
Cannot convert the “FileOnRoot.txt” value of type “System.String” to type “System.Collections.ArrayList”.

With 2 or more files, no problem… I have even tried setting the type when defining the variable ([System.Collections.ArrayList]$myArray = …)

“Dilemma” :slight_smile:

And that’s a pretty common “gotcha” in PowerShell. You’re going to have to check and see if only one item is selected, and create an array list from it, rather than trying to assign a string directly into the empty array list. That’s just kinda how .NET rolls.

Not having the whole code, i could not test, but try:

    $okButton.Add_Click(
        {
            [System.Collections.ArrayList]$outList = @($objListBox.CheckedItems)
            $subForm.Close()
        })

You should then be able to test for ($outList.Count -gt 0)

Well, it works before the return… :frowning:

Maybe I am doing that part wrong? Sorry I’ve been jumping between projects, but I am greatful for the ansers. Here is the full button code:

    $okButton = New-Object System.Windows.Forms.Button
    $okButton.Location = New-Object System.Drawing.Size(75,420)
    $okButton.Size = New-Object System.Drawing.Size(75,23)
    $okButton.Text = "OK"
    $okButton.Add_Click(
        {
        [System.Collections.ArrayList]$outList = @($objListBox.CheckedItems)
        [System.Windows.Forms.MessageBox]::Show($outList.Count)
        $subForm.Close()
        return $outList
        })
    $subForm.Controls.Add($okButton)

Now I believe with some output testing, the problem seems to be where I have my Return? I am instead going to pass the array as a param, but if someone wants to share with me how to do a return from a button… :slight_smile:

Thanks.

Ok. Still having problems here… and I am not picking up where said problem is occurring… I pass the Array through param:

param ([System.Collections.ArrayList] $theList, [String]$desc, [System.Collections.ArrayList]$outList)

I set and test the value of $outList before and after it is populated with the values from the checkboxlist. All is good. But out side of the function, the value is not changed… Why?

Ok, solved this business. I looked around and found many a reference to scope, [ref], return, write-host, and lastly $script:. The later seems to be the only one that worked for me. Funny, they all worked in quick 15 line scripts, but maybe it has to do with working in a subform and have the return come after a button press to close out the subform.

This is the line that worked for me:

    $okButton.Add_Click(
        {
        
        [System.Collections.ArrayList]$script:outList = @($objListBox.CheckedItems)
        $subForm.Close()
        })

Thanks for the assist with this.

The only thing I can think of is to close the form in code where you create / show it (not from within the button on the form) - you might be closing it before returning values?