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.
Locked
User avatar
morgange
Posts: 15
Joined: Mon Nov 07, 2016 9:38 am

Access textbox contents on formclose

Post by morgange » Thu Dec 27, 2018 9:55 am

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

User avatar
jvierra
Posts: 13392
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Access textbox contents on formclose

Post by jvierra » Thu Dec 27, 2018 11:11 am

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
Joined: Mon Nov 07, 2016 9:38 am

Re: Access textbox contents on formclose

Post by morgange » Thu Dec 27, 2018 1:34 pm

Quick and to the point.

I was just missing the last line..
Thank you once again jvierra

Locked