Powershell Multiline textbox output

Viewed 3784

Using the powershell code below I am trying to output separate lines but for some reason only the last line displays. Any way to get to show multiple lines in the textbox?

$objTextBox3 = New-Object System.Windows.Forms.TextBox 
$objTextBox3.Multiline = $True;
$objTextBox3.Location = New-Object System.Drawing.Size(10,160) 
$objTextBox3.Size = New-Object System.Drawing.Size(260,100) 
$objTextBox3.Scrollbars = "Vertical" 
$objForm.Controls.Add($objTextBox3)

$objTextBox3.Text = "TEST-" + $objTextBox2.Text + " FirstLine" + " (P) r`n  TEST-" + $objTextBox2.Text + "SecondLine" + " (P)"

Thought using r`n would work?

TIA

1 Answers

Mike Shepard has provided the crucial pointer in a comment on the question:

Your string was missing ` before the r in order to make `r`n` expand to a CRLF newline - that is, a Windows-style line break composed of a CR character ("`r") immediately followed by a LF character ("`n").

Thus:

$objTextBox3.Text = "TEST-" + $objTextBox2.Text + " FirstLine" + " (P) `r`n  TEST-" + 
                    $objTextBox2.Text + "SecondLine" + " (P)"

Note that the spaces around `r`n are retained.

Unlike PowerShell itself - which generally accepts CRLF, LF, and CR newlines interchangeably - the WinForms [System.Windows.Forms.TextBox] control with .Multiline set to $True only recognizes CRLF sequences as newlines in terms of display.

A LF(-only) newline - which your string mistakenly, but in effect used - is quietly ignored when displaying the string in the GUI (though it is retained in the control's .Text property).

Related