3

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

2
  • 2
    Did you mean to use a backtick before the r as well? Commented Aug 27, 2018 at 20:23
  • Thank you I was missing that Commented Aug 27, 2018 at 20:32

1 Answer 1

3

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).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.