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
Cannot close form with $FormName.Close()
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.
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.
- PGomersall
- Posts: 131
- Last visit: Thu Feb 15, 2024 12:14 pm
Cannot close form with $FormName.Close()
- Attachments
-
- Rename-Computer.psf.psbuild
- (3.95 KiB) Downloaded 96 times
-
- Rename-Computer.psf
- (63.68 KiB) Downloaded 87 times
Last edited by PGomersall on Tue Apr 06, 2021 11:56 am, edited 1 time in total.
- Alexander Riedel
- Posts: 8478
- Last visit: Tue Mar 26, 2024 8:52 am
- Been upvoted: 37 times
Re: Cannot close form with $FormName.Close()
[Topic moved by moderator]
Alexander Riedel
SAPIEN Technologies, Inc.
SAPIEN Technologies, Inc.
- PGomersall
- Posts: 131
- Last visit: Thu Feb 15, 2024 12:14 pm
Re: Cannot close form with $FormName.Close()
Alex, this issue is a problem with PSS rather than a GUI form, why did you move it?
Pete
Pete
- Alexander Riedel
- Posts: 8478
- Last visit: Tue Mar 26, 2024 8:52 am
- Been upvoted: 37 times
Re: Cannot close form with $FormName.Close()
Because it is not a problem with PSS
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
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.
SAPIEN Technologies, Inc.
Re: Cannot close form with $FormName.Close()
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.
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.
- Alexander Riedel
- Posts: 8478
- Last visit: Tue Mar 26, 2024 8:52 am
- Been upvoted: 37 times
Re: Cannot close form with $FormName.Close()
That'll teach you to run someone else's code Jim
Alexander Riedel
SAPIEN Technologies, Inc.
SAPIEN Technologies, Inc.
Re: Cannot close form with $FormName.Close()
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.
It is a good way to have a development machine behave since crashes can be quite normal although a VM is a better testbed.
- PGomersall
- Posts: 131
- Last visit: Thu Feb 15, 2024 12:14 pm
Re: Cannot close form with $FormName.Close()
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.
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.
- Alexander Riedel
- Posts: 8478
- Last visit: Tue Mar 26, 2024 8:52 am
- Been upvoted: 37 times
Re: Cannot close form with $FormName.Close()
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.
If the Form.Close() function would have been named Form.TriggerCloseEvent() or something like that, it would have been easier to guess.
Alexander Riedel
SAPIEN Technologies, Inc.
SAPIEN Technologies, Inc.