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?
Unhandled Exception when using the Close() method on a Form
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.
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.
Unhandled Exception when using the Close() method on a Form
- Attachments
-
- exception.txt
- full error text
- (11.91 KiB) Downloaded 91 times
-
- points to a filter script error
- unhandled.png (15.23 KiB) Viewed 2164 times
- mxtrinidad
- Posts: 399
- Last visit: Tue May 16, 2023 6:52 am
Re: Unhandled Exception when using the Close() method on a Form
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.
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
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
}
$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
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!!
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!!