async update GUI text

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 5 years and 4 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
sctsprin
Posts: 4
Last visit: Thu May 18, 2023 7:42 pm

async update GUI text

Post by sctsprin »

Hi, I've borrowed this powershell studio form code from the forum to run a background task then display the result in a textbox,
However the textbox flashes the result, I would like the result to remain until the next updated value is displayed,
Could someone please help?

Windows 10, Powershell studio 2018, v5.5.155

thanks
james
Attachments
updateGUI.psf
(21.44 KiB) Downloaded 90 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: async update GUI text

Post by jvierra »

Change this line:
$textbox1.Text = $results

To this:
$textbox1.Text += $results
sctsprin
Posts: 4
Last visit: Thu May 18, 2023 7:42 pm

Re: async update GUI text

Post by sctsprin »

Thanks for the reply, but that ends up just adding to the last result, so i end up with "12345...." etc
I want to only see the current text result displayed until it's replaced by the new result, so "1" is displayed until it's replaced by "2" etc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: async update GUI text

Post by jvierra »

If the updates happen too fast then it will always look funny.

What is the purpose of just outputting numbers as fast as you can?

Code: Select all

		Param ($Job)
		if($results = Receive-Job -Job $Job | Select-Object -Last 1){
		    $textbox1.Text = $results
        }
sctsprin
Posts: 4
Last visit: Thu May 18, 2023 7:42 pm

Re: async update GUI text

Post by sctsprin »

I actually just want to refresh once a minute, but need the last result to display until the next update, which currently isn’t happening, instead it’s flashing for a split second and is then empty u til the next flash update
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: async update GUI text

Post by jvierra »

The code I posted does exactly that you just need to set the timer to the value you want.
sctsprin
Posts: 4
Last visit: Thu May 18, 2023 7:42 pm

Re: async update GUI text

Post by sctsprin »

thanks, i found my mistake, i removed the if statement surrounding the textbox1.text = $results, which caused empty data to be displayed

thanks
james
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: async update GUI text

Post by jvierra »

That is not what is happening. The "if statement prevents a null value from being assigned to the text which was the initial issue with your code. Displaying once a second comes when the sleep is set to 60 seconds and not 1000 milliseconds.
This topic is 5 years and 4 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