Brick
Brick

Reputation: 119

Powershell GUI Imported text file missing newline/carriage return

I have a powershell GUI which imports a text file and displays it in a textbox when a button is clicked

But even though the text file contains one entry per line when it gets displayed in the textbox it is all on one line...

The text file looks like this-

TXT

But when I import it it looks like this-

GUI

This is the code I am using-

$button_hosts = New-Object system.windows.Forms.Button
$button_hosts.Text = "Hosts"
$button_hosts.Width = 60
$button_hosts.Height = 25
$button_hosts.location = new-object system.drawing.point(20,55)
$button_hosts.Font = "Microsoft Sans Serif,10"
$mydocs = [Environment]::GetFolderPath('MyDocuments')
$button_hosts.Add_Click({
    $textBox_hosts.Text = Get-Filename "$mydocs" txt
    $textBox_hostlist.Text = Get-Content $textBox_hosts.Text
})
$GUI.controls.Add($button_hosts)

Any idea how to get it to display the same? I cant add any extra data to the txt file as it is an output from another program

Upvotes: 2

Views: 1214

Answers (3)

thepip3r
thepip3r

Reputation: 2935

For your WinForms TextBox, do you have the multiline property set to true?

https://msdn.microsoft.com/en-us/library/12w624ff(v=vs.110).aspx

If not, it defaults to single line.

Upvotes: 0

TessellatingHeckler
TessellatingHeckler

Reputation: 29033

Set the lines property, not the text property.

$textBox_hostlist.Lines = Get-Content $textBox_hosts.Text

Upvotes: 1

iRon
iRon

Reputation: 23788

Get-Content reads the content one line at a time and returns a collection of objects, each of which represents a line of content.
The means that you have to join the collection with carriage returns and linefeeds:

(Get-Content $textBox_hosts) -Join "`r`n"

Upvotes: 0

Related Questions