3

In my PowerShell script I display a form and I use the $Form.Close(); command to close it. It closes fine however when it does so it returns the word Cancel (it also does this if I manually close the form by pressing the top-right red-x).

This was OK when I ran the script in the console but now I'm using PS2EXE-GUI to run my scripts as a GUI executable when $Form.Close() runs a pop-up msg appears with just the word Cancel which the user then has to click OK to get rid of. Is there any way to close a form silently please?

Complete form code on my blog here

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
Richie W
  • 31
  • 1
  • 3
  • I don't work in powershell, but I'd assume you need to set DialogResult to ok. something like: $form.DialogResult = [System.Windows.Forms.DialogResult]::OK – Troy Mac1ure Apr 04 '17 at 16:18
  • Hi Troy, Thanks for responding. Your answer looks promising but unfortunately itdoesn't seem to work for me, the result is still "Cancel". Looking at the MSDN page on DialogResult Enumeration it suggests $Form.DialogResult = [System.Windows.Forms.DialogResult]::None should work (I don't want any output) but it doesn't - I also tried other options i.e Yes, No OK etc, still only "Cancel" is returned. I can paste my form code below if there's space – Richie W Apr 04 '17 at 17:09
  • I added $Form.DialogResult = [System.Windows.Forms.DialogResult]::None to the section where I define the form - is this the correct location? – Richie W Apr 04 '17 at 17:12

3 Answers3

1

I run into this problem today, and I solved it by using the Out-Null cmdlet after ShowDialog method:

$Form.ShowDialog() | Out-Null
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
David.P
  • 11
  • 1
0

I usually do:

[void]$Form.Close()

But you could also do:

$Form.Close() | Out-Null
sodawillow
  • 12,497
  • 4
  • 34
  • 44
  • Hi, thanks for your reply. I already tried these but they have no effect unfortunately. The scripts runs as before, when the form closes it returns "Cancel" – Richie W Apr 04 '17 at 19:53
  • @RichieW can you please [edit your question](http://stackoverflow.com/posts/43211375/edit) and provide the script, or if it is too long, the main parts of it? – sodawillow Apr 04 '17 at 19:57
  • I've pasted the code here: https://albarich.wordpress.com/2017/03/31/script-close-form-after-certain-time/ – Richie W Apr 04 '17 at 19:59
  • I tried the code and both methods make the closing silent on my computer : ). – sodawillow Apr 04 '17 at 20:02
  • I've added a screenshot of the console to the URL above. When I run the exact code in ISE (or standard console) it opens the form, closes and returns "Cancel". I'm using PS 4 on 8.1, what's your env please? – Richie W Apr 04 '17 at 20:25
  • In ISE, PS5 on W10. The form does not close by itself here, I have to use Alt + F4 (don't know why, I haven't dived into the code yet). – sodawillow Apr 04 '17 at 20:39
0

I found another way of producing the time-limited forms that don't return a DialogResult. I've updated my page here with the updated code.

Richie W
  • 31
  • 1
  • 3