form1_Shown having unexpected behavior

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 9 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
kilo1548
Posts: 15
Last visit: Tue Oct 18, 2022 2:23 pm

form1_Shown having unexpected behavior

Post by kilo1548 »

To help you better we need some information from you.

*** Please fill in the fields below. If you leave fields empty or specify 'latest' rather than the actual version your answer will be delayed as we will be forced to ask you for this information. ***

Product, version and build:
32 or 64 bit version of product: PowerShell Studio 2017 v5.4.145 x64
Operating system:
32 or 64 bit OS: Win 10 Pro x64

*** Please add details and screenshots as needed below. ***

When running the form1_Shown event handler I am getting unexpected results. According to the MSDN the form_Shown event handler for a form Occurs whenever the form is first displayed.
msdn.png
msdn.png (3.51 KiB) Viewed 2307 times
However, With the following code:

Code: Select all

$form1_Shown = {
		sleep -Milliseconds 3000
		[void][System.Windows.Forms.MessageBox]::Show('The application could not be started.  Please contract your IT administator', 'Error')
}
I am getting a three second pause, then the MessageBox and then the form is displaying on the screen.

Should I be using a different event for this if the end Goal is to have the form displayed to the user wait three seconds and then have the MessageBox appear?
cody m

Re: form1_Shown having unexpected behavior

Post by cody m »

Make sure you have the right event handlers in the correct events, if you have $form1_Shown in the load event you will get the behavior that you are experiencing.
eventhandler.png
eventhandler.png (56.35 KiB) Viewed 2300 times
User avatar
kilo1548
Posts: 15
Last visit: Tue Oct 18, 2022 2:23 pm

Re: form1_Shown having unexpected behavior

Post by kilo1548 »

That solved the first issue. Now I am running into an issue where the form is shown to the user but the progress bar marquee is not starting until after the timer ends and the MessageBox is displayed.

Can the Progress Bar marquee begin as soon as the form is displayed to the end user then have the code run?
cody m

Re: form1_Shown having unexpected behavior

Post by cody m »

[TOPIC MOVED BY MODERATOR]
I moved the topic to our PowerShell GUIs forum so that other users can chime in.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: form1_Shown having unexpected behavior

Post by jvierra »

kilo1548 wrote: Mon Jun 25, 2018 3:11 pm That solved the first issue. Now I am running into an issue where the form is shown to the user but the progress bar marquee is not starting until after the timer ends and the MessageBox is displayed.

Can the Progress Bar marquee begin as soon as the form is displayed to the end user then have the code run?
Hi kilo, Not sure what you are trying to do. You can start a ProgressBar update after the form is shown or activated and it will display. Note that that is "after" those two events.

Perhaps you want to use a timer to run the ProgressBar.
User avatar
kilo1548
Posts: 15
Last visit: Tue Oct 18, 2022 2:23 pm

Re: form1_Shown having unexpected behavior

Post by kilo1548 »

I would like the progress bar to continue to move back and fourth while the while the rest of the powershell completes. Right now it will load and move just a little bit with the additional of the $progressbar1.Update() but as soon as it moves to the next line of the powershell it will stop moving the marquee progress bar while continuing to run the rest of the powershell.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: form1_Shown having unexpected behavior

Post by jvierra »

Yes. This is how Windows Forms work. Events are blocking. Any code executing blocks all messages that are needed to activate controls. To prevent this you need to use a "job" for your script.

Use the JobTracker control set to make this work as you want.
This topic is 5 years and 9 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