Cannot close form with $FormName.Close()

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
User avatar
PGomersall
Posts: 106
Last visit: Fri Apr 16, 2021 7:00 pm

Cannot close form with $FormName.Close()

Post by PGomersall »

Product: PowerShell Studio 2021 (64 Bit)
Build: v5.8.187
OS: Windows 10 Education (64 Bit)
Build: v10.0.19043.0
I have a single form (FixedDilog) which basically is to rename and or join a computer to our domain.
I have added a bunch of code but been having problems with If\Else statements that have the $FormName.Close() in them; this statement is basically ignored. I did some more testing and placed the close statement as the first line of the OK button click event and then ran the form. When I click the OK button the close statement is ignored. Not sure what is going on. I attach the psf file. The $FormName.Close() statement is first line of OK button event.
Can you try and see what the problem is?
Thanks Pete.

Just tested this with a brand new form with single button and following code in the psf:
$button1_Click = {
#Add-Type -AssemblyName "System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
[void][System.Windows.Forms.MessageBox]::Show("Close form is next", 'Title') # Casting the method to [void] suppresses the output.
$form1.Close()
#Add-Type -AssemblyName "System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
[void][System.Windows.Forms.MessageBox]::Show("Close form did not happen", 'Title') # Casting the method to [void] suppresses the output.
}
Running the form with PSS and clicking button1 -
$form1.Close() never fires and I always get the second msgbox
Attachments
Rename-Computer.psf.psbuild
(3.95 KiB) Downloaded 7 times
Rename-Computer.psf
(63.68 KiB) Downloaded 9 times
Last edited by PGomersall on Tue Apr 06, 2021 11:56 am, edited 1 time in total.

User avatar
Alexander Riedel
Posts: 7665
Last visit: Wed Apr 21, 2021 12:28 am
Answers: 3
Been upvoted: 8 times

Re: Cannot close form with $FormName.Close()

Post by Alexander Riedel »

[Topic moved by moderator]
Alexander Riedel
SAPIEN Technologies, Inc.

User avatar
PGomersall
Posts: 106
Last visit: Fri Apr 16, 2021 7:00 pm

Re: Cannot close form with $FormName.Close()

Post by PGomersall »

Alex, this issue is a problem with PSS rather than a GUI form, why did you move it?
Pete

User avatar
Alexander Riedel
Posts: 7665
Last visit: Wed Apr 21, 2021 12:28 am
Answers: 3
Been upvoted: 8 times

Re: Cannot close form with $FormName.Close()

Post by Alexander Riedel »

Because it is not a problem with PSS :D
It is a misconception how Windows Forms and events work. To iterate for everyone reading, PowerShell Studio does not change, influence or negate any part of Microsoft Windows Forms.
Microsoft Windows Forms are part of the Microsoft .NET framework or .NET Core 3.x or above.

As to your problem, you are triggering a close event by calling Form.Close from another event handler.
That adds the 'close' event to the event queue. Any new events will not be handled until you either
exit the current event handler or call DoEvents(), which in this case you should not.
Simply returning from the event handler after calling Close() should suffice.

For reference please also see:
https://stackoverflow.com/questions/391 ... e-the-form
Alexander Riedel
SAPIEN Technologies, Inc.

jvierra
Posts: 14762
Last visit: Mon Apr 19, 2021 4:16 pm
Answers: 9
Has voted: 3 times
Been upvoted: 7 times

Re: Cannot close form with $FormName.Close()

Post by jvierra »

Place a return statement at the beginning of the load event then comment out all message boxes in the Close button event. The close will happen. Same for the other event.

It is also a bad idea to restart the local system from a form. It can be done but it can also cause a system exception. I ran your form and didn't think and answered yes. The form closed then the system threw an exception and restarted. I should have thought first.

User avatar
Alexander Riedel
Posts: 7665
Last visit: Wed Apr 21, 2021 12:28 am
Answers: 3
Been upvoted: 8 times

Re: Cannot close form with $FormName.Close()

Post by Alexander Riedel »

That'll teach you to run someone else's code Jim :D
Alexander Riedel
SAPIEN Technologies, Inc.

jvierra
Posts: 14762
Last visit: Mon Apr 19, 2021 4:16 pm
Answers: 9
Has voted: 3 times
Been upvoted: 7 times

Re: Cannot close form with $FormName.Close()

Post by jvierra »

No. It reminded me to look before I leap. Luckily my machine recovers to exactly the point of the crash and all apps come open and back where they were although the restart is very slow.

It is a good way to have a development machine behave since crashes can be quite normal although a VM is a better testbed.

User avatar
Lembasts
Posts: 321
Last visit: Thu Apr 15, 2021 3:39 pm

Re: Cannot close form with $FormName.Close()

Post by Lembasts »

If you just wanted to terminate everything, couldnt you use:
[environment]::exit(0)
?

User avatar
PGomersall
Posts: 106
Last visit: Fri Apr 16, 2021 7:00 pm

Re: Cannot close form with $FormName.Close()

Post by PGomersall »

Thank you all for your input, I learned something about win forms event processing.
Alex, apologies for my lack of understanding and my consequent thought that it was a problem of PSS.
So regarding the reboot from the form; the form will be used after imaging a system that has been done manually rather than with sccm. It is called from the last line of SetupComplete.cmd. Given that there will be no user applications in the picture I believe the risks if any will be minimal. This is still in the development stage so we will see when we come to test it. The form does give the technician time to complete other tasks on the system before committing to the reboot, either from the form or manually.

User avatar
Alexander Riedel
Posts: 7665
Last visit: Wed Apr 21, 2021 12:28 am
Answers: 3
Been upvoted: 8 times

Re: Cannot close form with $FormName.Close()

Post by Alexander Riedel »

No need to apologize. You are not alone. It is not always easy to understand if a function executes something immediately or triggers an event to be queued.
If the Form.Close() function would have been named Form.TriggerCloseEvent() or something like that, it would have been easier to guess. :D
Alexander Riedel
SAPIEN Technologies, Inc.

Post Reply