How to stop Dialogresult closing your Powershell GUI application

04 December,2019 by Jack Vamvas

Question: My head has officially exploded. I've been working on a Powershell script which includes creating a power GUI using the  System.Windows.Forms.Form api.

Part of the GUI includes clicking on a button which generates a Child Form . The Child Form has a DialogResult associated on the OK button - 

$ChildfOKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK

The problem is every time I click the OK button on the Child Form , it not only closes the Child Form - but also closes the Parent Form. This is not the behaviour I'm expecting or require. 

How can I fix?

Answer: In short the fix is to reset the value of the DialogResult to None. The application will close if you set the value to CancelAbortRetryIgnoreYes, or No. Once you realise this is the behaviour - suddenly it all becomes clearer. When I first started working with DialogResult - I also experienced this problem . 

 

it does help to understand the uses of DialogResult

First use - standardisation. By having something like : [System.Windows.Forms.DialogResult]::OK  , when the .Net programmers make changes - they only need to focus on the behaviour. It won't be necessary to go and rewrite the click events. 

Second use - After your experience - you may think the only use of DialogResult is to close the application. Really it's purpose is to communicate to the Parent form - what action the Child Form has taken . 

 

Powershell is a vast subject - the more you know the better - Expand your Powershell mind – Three key cmdlets

 

 


Author: Jack Vamvas (http://www.sqlserver-dba.com)


Share:

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment on How to stop Dialogresult closing your Powershell GUI application


sqlserver-dba.com | SQL Server Performance Tuning | SQL Server DBA:Everything | FAQ | Contact|Copyright & Disclaimer