Search code examples
winformspowershellsplittextboxrichtextbox

Output multiline textbox to variable in PowerShell WinForm


I have a WinForm in PowerShell where email contents are copied into a multiline textbox, output to a variable, and sent to a recipient on button press.

Everything works perfectly except the content from the textbox does not remain multiline.

Is it possible to extract the entered text and output in the same format?

I've attempted using TextBox and RichTextBox, splitting etc but unable to get it to work.

Thanks


Solution

  • You could try pulling $textBox.Lines instead of $textBox.Text. This will give you an array of textlines in the box you can join together with the appropriate newline:

    # for normal text:
    $textEntered = $textBox.Lines -join [Environment]::NewLine
    
    # for HTML:
    $textEntered = $textBox.Lines -join '<br />'