Access textbox contents on formclose

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

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 5 years and 2 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.
Locked
User avatar
morgange
Posts: 15
Last visit: Sun Jan 22, 2023 1:14 pm

Access textbox contents on formclose

Post by morgange »

Hi,

I have a GUI that writes all sorts of information to a textbox and i would like to write the contents of the textbox to an event log on form close.
I have an exit button that works fine but when using the red 'X' its a different matter.
Ideally i want to keep the 'contolbox' object.

I have the following that i found on another question which works fine in principal. They both detect the formclosing and i can create a text file etc.
My issue is that its not able to get the textbox text. what am i missing?

1:
$form1_FormClosing=[System.Windows.Forms.FormClosingEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.FormClosingEventArgs]
$logtext = $TextBox1.text
add-content "c:\dump\admingui_closed1.txt" $logtext
}

2:
Register-ObjectEvent -InputObject $form1 -EventName FormClosing -Action {

$logtext = $TextBox1.text
add-content "c:\admingui_closed2.txt" $logtext
#Write-EventLog -LogName "Admin GUI" -Source "Admin GUI" -EventId 666 -Message $logtext

}

Cheers

Geoff
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Access textbox contents on formclose

Post by jvierra »

Register-ObjectEvent does not work with forms. You must add the event to the form.

Code: Select all

$form1_FormClosing = [System.Windows.Forms.FormClosingEventHandler]{
    #Event Argument: $_ = [System.Windows.Forms.FormClosingEventArgs]
    $logtext = $TextBox1.text
    add-content "c:\dump\admingui_closed1.txt" $logtext
}
$form1.add_FormClosing($form1_FormClosing)
If you are using PowerShell studio then just use the properties/events panel to add the event.
User avatar
morgange
Posts: 15
Last visit: Sun Jan 22, 2023 1:14 pm

Re: Access textbox contents on formclose

Post by morgange »

Quick and to the point.

I was just missing the last line..
Thank you once again jvierra
This topic is 5 years and 2 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.
Locked