How do I suppress my event-handling function from emitting unwanted text?

Multi tool use


How do I suppress my event-handling function from emitting unwanted text?
I have a PowerShell script that composes a WinForms UI. Here are the pertinent bits:
cls
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
Function FormClosed()
{
$global:Canceled = $true
}
$global:MainForm = New-Object 'System.Windows.Forms.Form'
$global:MainForm.add_FormClosed({FormClosed})
$global:MainForm.ShowDialog()
When I run the program, I get a PowerShell console window and the UI (as expected). When I click the main form control box "X", the function FormClosed
is invoked (as expected). However, the text Cancel
unexpectedly appears in the console window.
FormClosed
Cancel
I put a breakpoint on $global:Canceled = $true
in the function FormClosed
. At this breakpoint, I step and then I see the execution pointer is on the }
in $global:MainForm.add_FormClosed({FormClosed})
. I step one more time and the text Cancel
appears in the console window. So, I assume that the .NET FormClosed
event is firing my FormClosed
function (as expected) and my FormClosed
function is emitting the text Cancel
. I assume the text Cancel
originated from the .NET FormClosed
event.
$global:Canceled = $true
FormClosed
}
$global:MainForm.add_FormClosed({FormClosed})
Cancel
FormClosed
FormClosed
FormClosed
Cancel
Cancel
FormClosed
How do I suppress my FormClosed
function from emitting the text Cancel
?
FormClosed
Cancel
1 Answer
1
Cancel
is return value from ShowDialog
method. You can send it to Null output:
Cancel
ShowDialog
$global:MainForm.ShowDialog() | Out-Null
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.