Looping Through Values in Multiline TextBox

Product: PowerShell Universal
Version: 2.7.0

Hi,

Sorry if i have missed something super simple but i have a multi-line textbox that i would like to read in a loop line by line, i chanced the following code but this didn’t work and can’t see anything in the docs about this.

New-UDForm -Content {
    New-UDTextBox -Id 'txtTo' -Label 'To' -Multiline -Rows 4 -RowsMax 10
} -OnSubmit {

$EventData.txtTo | ForEach-Object {

    Show-UDToast -Message $_
}

It appears to pass everything in the textbox as one line though rather than reading them separately as i’d hoped.

Is there a way to do this?

I will be replacing the Toast Message with a function to process each line, i just used the toast message to see what was being passed.

Cheers,
Jamie

There may be a better way but splitting on new line worked when I just tested.

New-UDForm -Content {
        New-UDTextBox -Id 'txtTo' -Label 'To' -Multiline -Rows 4 -RowsMax 10
} -OnSubmit {
        $EventData.txtTo.Split([Environment]::NewLine) | ForEach-Object {
            Show-UDToast -Message $_
        }
}    

Thank you for your reply.

I can’t seem to get it to split, I copied your code but I still get the output as one line

image

The issue is these lines will be mobile numbers and as I get them as a single line I get an invalid number.

Are you getting them as separate lines?

yes it works for me, here is the full page code. Your example was missing a } for the foreach. may have just been a copy and past issue.

New-UDDashboard -Title "Hello, World!" -Content {
    New-UDTypography -Text "Hello, World!"

    New-UDForm -Content {
        New-UDTextBox -Id 'txtTo' -Label 'To' -Multiline -Rows 4 -RowsMax 10
    } -OnSubmit {
        $EventData.txtTo.Split([Environment]::NewLine) | ForEach-Object {
            Show-UDToast -Message $_
        }
    }                       
}

Mike

1 Like

That’s very strange, i just copied the full code into a new dashboard and ran it and still get the single toast notification back.

Are you using the same version as me (2.7.0)?

2.7.1
Are you typing each line and pressing enter? Just want to make sure.
Mike

Yes, after each line of text, im pressing enter to go to a new line.

Regards,
Jamie

Ah, it works when running in 5.1 Environment, not 7+ though

1 Like

I actually can reproduce this. This fixed it for me.

New-UDDashboard -Title "Hello, World!" -Content {
    New-UDTypography -Text "Hello, World!"

    New-UDForm -Content {
        New-UDTextBox -Id 'txtTo' -Label 'To' -Multiline -Rows 4 -RowsMax 10
    } -OnSubmit {
        $EventData.txtTo.Split("`n") | ForEach-Object {
            Show-UDToast -Message $_
        }
    }                       
}

When I did this, I noticed that the data was test\ntest\ntest rather than test\r\ntest\r\ntest so that’s when Environment.NewLine wasn’t working.

New-UDDashboard -Title "Hello, World!" -Content {
    New-UDTypography -Text "Hello, World!"

    New-UDForm -Content {
        New-UDTextBox -Id 'txtTo' -Label 'To' -Multiline -Rows 4 -RowsMax 10
    } -OnSubmit {
        Show-UDToast $Body
    }                       
}
3 Likes

Interesting its a diffrence between 5.1 and 7+. Would not have thought that would have changed. Sorry about that, did learn something new though :slight_smile:
Mike

1 Like

Using this to split it appears to work on the frontend but when passing to the API it reports an invalid number still

When i output the body like your example it looks like it isn’t outputting correctly, it’s not got the \r’s in them

Or maybe im misunderstanding something :slight_smile:

EDIT

Adding this screenshot in case it helps with real data, just obfuscated my number :slight_smile:

If i only type 1 number in then it works fine

Regards,
Jamie

Weird. Maybe try to convert the number to an int or something? trim() will also remove any leading and trailing characters that aren’t visible.

New-UDDashboard -Title "Hello, World!" -Content {
    New-UDTypography -Text "Hello, World!"

    New-UDForm -Content {
        New-UDTextBox -Id 'txtTo' -Label 'To' -Multiline -Rows 4 -RowsMax 10
    } -OnSubmit {
        $EventData.txtTo.Split("`n") | ForEach-Object {
             [int]$Number = $_.Trim()
             # Call API here
        }
    }                       
}

Ah, found the issue… Me!

Despite having split the number, i was still actually calling “$EventData.txtTo” in the API call, changing that to “$_” fixed it

Thank you both for your support.

@adam I absolutely love this software, I have wanted a web interface for so many scripts and always settled on creating Windows Forms but stumbled across PSU this morning and (now) moved one of my scripts over with little issue.

Thank you

3 Likes

Welcome to the club JBobbsy, we got it about a month ago. We have been centralizing a lot of our scripts and giving other people functionality they did not have before.
Great tool and cant wait to see where it goes!
Mike

3 Likes