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

This forum can be browsed by the general public. Posting is limited to current SAPIEN license holders with active maintenance and does not offer a response time guarantee.
Forum rules
DO NOT POST SUBSCRIPTION NUMBERS, LICENSE KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
matthmsft
Posts: 16
Joined: Mon Apr 17, 2017 7:11 pm

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

Post by matthmsft » Sun Aug 18, 2019 8:27 pm

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?
Attachments
exception.txt
full error text
(11.91 KiB) Downloaded 8 times
points to a filter script error
points to a filter script error
unhandled.png (15.23 KiB) Viewed 798 times

User avatar
mxtrinidad
Site Admin
Posts: 359
Joined: Sun Mar 03, 2013 12:42 pm

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

Post by mxtrinidad » Mon Aug 19, 2019 6:17 am

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.

:)

User avatar
matthmsft
Posts: 16
Joined: Mon Apr 17, 2017 7:11 pm

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

Post by matthmsft » Tue Aug 20, 2019 5:40 am

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

}

User avatar
matthmsft
Posts: 16
Joined: Mon Apr 17, 2017 7:11 pm

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

Post by matthmsft » Tue Aug 20, 2019 5:50 am

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!!