TextBox/RichTextBox new lines for imported text file

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 6 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
PsCustomObject
Posts: 137
Last visit: Mon Mar 18, 2024 3:11 am

TextBox/RichTextBox new lines for imported text file

Post by PsCustomObject »

Product, version and build: PowerShell Studio 2017 build 5.4.140
32 or 64 bit version of product: 64
Operating system: Win10 Ent.
32 or 64 bit OS: 64
PowerShell Version: 5.1

As per subject I'm working a GUI project to parse some log files for specific strings, logs in turn are generated by other PowerShell scripts with a small function which stamps the time message has been logged together with the message.

New line in the log file is handled through " `r`n", in the GUI I let the user select the folder where to look for logs, select the string to search and the ouput content to the TextBox (same behavior I've though experienced with a RichTextbox), problem is new lines are not honored and text looks inconsistent.

I've tried multiple approaches but result is always the same.

- Multiline is of course on
- I've tried disabling word wrap
- I've tried splitting the string
- Some other things not coming to mind :-)

Obviousely I'm getting this wrong and would really appreciate if anybody could point me toward the right direction, to keep in mind user has the ability to load the whole log file if she/he choses to do so but result does not change so it is not my mangling the string introducing the issue.

Thanks in advance for any help!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: TextBox/RichTextBox new lines for imported text file

Post by jvierra »

$textbox1.Lines = Get-Content file.log

This maintains the line breaks.

When creating a file in PowerShell use Out-File -append and don't add line breaks. Out-File adds them automatically and correctly.
User avatar
PsCustomObject
Posts: 137
Last visit: Mon Mar 18, 2024 3:11 am

Re: TextBox/RichTextBox new lines for imported text file

Post by PsCustomObject »

Thanks but all of this is already in place and the source of my headache :-)

Line breaks in this specific context are a necessity as I'm storing log's entries in a variable and then sending that to the log file at the end of a chain of scripts, reason for this appraoch is everything is implemented through Orchestrator and need to fork/switch path according to script's output so having log in a variable is easy to pass down the databus.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: TextBox/RichTextBox new lines for imported text file

Post by jvierra »

If there are line breaks in the file then the variable will be an array.

$variable = Get-Content file.log
$textbox1.Lines = $variable


There is no other way to do this. If you assign to Text the text will get clobbered.
User avatar
PsCustomObject
Posts: 137
Last visit: Mon Mar 18, 2024 3:11 am

Re: TextBox/RichTextBox new lines for imported text file

Post by PsCustomObject »

Thanks a lot, I will give this a try and see... worst case I will reowrk a bit the log functionality or simply call an external tool for parsing the file as anyhow this is for internal use by 1st level support.

As usual appreciate your time helping out.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: TextBox/RichTextBox new lines for imported text file

Post by jvierra »

Mostly there is not enough information to know what it is that you are having an issue with. If you are parsing the file incorrectly then you will lose all lines or cause the output to be scrambled.

A multiline textbox is an array of lines. If you feed it text only it will not be an array but will be a large block of text. It does not work exactly like a RichTextBox.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: TextBox/RichTextBox new lines for imported text file

Post by jvierra »

Here is a good example of how a textbox works and how it can screw up lines.
Attachments
Test-TxtLength.psf
(27.07 KiB) Downloaded 296 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: TextBox/RichTextBox new lines for imported text file

Post by jvierra »

Here is an alternate way to keep from losing the lines:
Attachments
Test-TextLoad.psf
(30.1 KiB) Downloaded 327 times
This topic is 6 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked