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.
Post Reply
minimen456
Posts: 5
Last visit: Sat Apr 17, 2021 3:25 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 8 times
Capture1.PNG
Capture1.PNG (8.6 KiB) Viewed 312 times

jvierra
Posts: 14762
Last visit: Mon Apr 19, 2021 4:16 pm
Answers: 9
Has voted: 3 times
Been upvoted: 7 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: Sat Apr 17, 2021 3:25 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: 14762
Last visit: Mon Apr 19, 2021 4:16 pm
Answers: 9
Has voted: 3 times
Been upvoted: 7 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)

Post Reply