Print results into a textbox in reverse order

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 2 years and 11 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
minimen456
Posts: 5
Last visit: Tue Apr 27, 2021 4:04 am
Has voted: 1 time

Print results into a textbox in reverse order

Post by minimen456 »

Hello,
I'd like to print results into a textbox in reverse order. I'm clicking a button and the newer datestamp must be on top. I've tried like this with no success. Is there a proper way?

Code: Select all

	$txtResult.Lines += "$(get-date -Format "hh:mm:ss")"
	$tmp = $txtResult.Lines
	[array]::Reverse($tmp)
	$txtResult.Lines = $tmp
Attachments
get-windowsversion_.psf
(38.67 KiB) Downloaded 102 times
Capture1.PNG
Capture1.PNG (8.6 KiB) Viewed 2368 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Print results into a textbox in reverse order

Post by jvierra »

There is no reverse order for an array. You can create a new array in reverse then assign it to the "Lines" property.

To insert lines in a list use a ListBox as inserts can be done at the top.

You can also do this.

$listbox.Text = $newline + $listbox.Text
minimen456
Posts: 5
Last visit: Tue Apr 27, 2021 4:04 am
Has voted: 1 time

Re: Print results into a textbox in reverse order

Post by minimen456 »

Does nothing, Listbox doesn't display anything:

Code: Select all

$listbox.Text = "something"
I seems You've meant:

Code: Select all

$texbox1.Text = "$(get-date -Format "hh:mm:ss")" + [Environment]::NewLine + $textbox1.Text
Thanks, the solution works. Also it seems I can't copy results to a clipboard from Listbox when the GUI is running.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Print results into a textbox in reverse order

Post by jvierra »

Sorry - typo. SHould be

$textbox.Text = $newline + $textbox.Text

For ListBox just insert the line at index 0.

$listbox.Items.Insert(0,$line)
This topic is 2 years and 11 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