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 LICENSE NUMBERS, ACTIVATION 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.
This topic is 4 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
User avatar
matthmsft
Posts: 16
Last visit: Fri Oct 02, 2020 12:31 am

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

Post 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?
Attachments
exception.txt
full error text
(11.91 KiB) Downloaded 89 times
points to a filter script error
points to a filter script error
unhandled.png (15.23 KiB) Viewed 2140 times
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

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

Post 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.

:)
User avatar
matthmsft
Posts: 16
Last visit: Fri Oct 02, 2020 12:31 am

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

Post 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

}
User avatar
matthmsft
Posts: 16
Last visit: Fri Oct 02, 2020 12:31 am

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

Post 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!!
This topic is 4 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.