Page 1 of 1

Unhandled Exception when using the Close() method on a Form

Posted: Sun Aug 18, 2019 8:27 pm
by matthmsft
Product: PowerShell Studio 2019 (64 Bit)
Build: v5.6.167
OS: Windows 10 Enterprise (64 Bit)
Build: v10.0.18362.0

I have a button in my application which allows the user to attest to an action being manually completed. The code is as follows:

$btn_ManualAttestOK_Click={
$paramWriteLogEntry = @{
logMessage = "btn_ManualAttestOK_Click() - Manual Attestation Closed"
logComponent = "btn_ManualAttestOK_Click()"
Txtbox = $txt_OutputWindow
}
Write-LogEntry @paramWriteLogEntry

$Script:ManualAttestationComplete = $true

$this.Parent.Close()
}

The problem is that when the form closes, an unhandled exception is generated. If I take out the form close event, everything works fine.

In the unhandled exception error it points to a Filter Script error - no idea what that is. See attachment.

I can upload an output log if it would help?

Re: Unhandled Exception when using the Close() method on a Form

Posted: Mon Aug 19, 2019 6:17 am
by mxtrinidad
You are missing adding the "FormClosed" event to the form.

Here's an example:

$form1_Load={
#TODO: Initialize Form Controls here

}

$button1_Click={
#TODO: Place custom script here
$textbox1.Text = "This is a CLose form test";
sleep -Seconds 5

$form1.Close()


}

$form1_FormClosed=[System.Windows.Forms.FormClosedEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.FormClosedEventArgs]
#TODO: Place custom script here

}

You do this by, in the form designer, right-click and "Add Events". Look for the "FormClosed" event. Then, the $form.close() should work.

:)

Re: Unhandled Exception when using the Close() method on a Form

Posted: Tue Aug 20, 2019 5:40 am
by matthmsft
Thanks for the pointer. It was working before but perhaps maybe that event got removed. I re-added it back and now my cancel button works fine, but my OK button doesn't, still has the exception message. Odd.

$btn_ManualAttestCancel_Click={

$paramWriteLogEntry = @{
logMessage = "btn_ManualAttestCancel_Click() - Manual Attestation Closed"
logComponent = "btn_ManualAttestCancel_Click()"
Txtbox = $txt_OutputWindow
}
Write-LogEntry @paramWriteLogEntry

$chk_ManualAttestConfirm.Checked = $false
$Script:ManualAttestationComplete = $false
$frm_ManualAttest.Close()

}

$btn_ManualAttestOK_Click={

$paramWriteLogEntry = @{
logMessage = "btn_ManualAttestOK_Click() - Manual Attestation Closed"
logComponent = "btn_ManualAttestOK_Click()"
Txtbox = $txt_OutputWindow
}
Write-LogEntry @paramWriteLogEntry

$chk_ManualAttestConfirm.Checked = $false
$Script:ManualAttestationComplete = $true
$frm_ManualAttest.Close()
}

$frm_ManualAttest_FormClosed=[System.Windows.Forms.FormClosedEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.FormClosedEventArgs]
#TODO: Place custom script here

}

Re: Unhandled Exception when using the Close() method on a Form

Posted: Tue Aug 20, 2019 5:50 am
by matthmsft
Got it! Your post pointed me in the right direction, thank you!

The problem wasn't the form close event, it was what happened immediately after the event when we returned to the main form, there was a broken XML filter.

Thanks!!