Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive

PowerShell Studio has a selection of Windows Form templates that you can use to speed up your development. You will most likely be using your own code snippets to add behind the form controls, as well as the snippets available in PowerShell Studio, and perhaps snippets from someone else. This is fine until you start trying to copy/paste an actual event code block.

Basic Button Control

There are many controls we can add to our Windows Form. The Button Control is the one you’ll click that will run your code behind the control and is the most used control in any Windows Forms application.

The image below shows some other controls added to a form:

WinEventHandler 01 2018 04 06 14 50 52

In the following example I am adding the “Message Box (Yes / No)” code from the PowerShell Studio snippet collection into the “Button1” control.  Simply double-click on the snippet and the code will be automatically added to the control:

WinEventHandler 02 2018 04 06 14 50 52

The result of adding the code inside the Button control is that when the form is executed, pressing “Button1” will execute the code behind the control. But, what’s really happening when you add any control to the form?

Control Event Handler Registration

Here’s where it gets interesting! Every time you drag/drop to add a control—the event handler for that control gets registered in the form. This is important and it’s done behind the scene. What will happened if you decide to delete the code block for the control?

In our example we remove the code block for the “Button1” control:

WinEventHandler 03 2018 04 06 14 50 52

WinEventHandler 04 2018 04 06 14 50 52

Removing the code doesn’t remove the “Button1” object from the GUI form:

WinEventHandler 06 2018 04 06 14 50 52

The following pop-up message will display when you manually remove the control code block:

WinEventHandler 07 2018 04 06 14 50 52

This indicates that removing the control code will unregister the Control Event Handler for that control in the form. And, once it is removed, copy/pasting the code back won’t work:

WinEventHandler 08 2018 04 06 14 50 52

WinEventHandler 09 2018 04 06 14 50 52

The good news—it’s easy to recover Control Event Handlers in PowerShell Studio!

Recovering Control Event Handlers

In order to register back the Control Event Handler:

  1. In the form Designer, select the control to re-register.
  2. In the Properties Panel, select the Events lightening-bolt button WinEventHandler 10 2018 04 06 14 50 52.
  3. In the Action section, select the Click event, followed by the control that was added back: “button1_Click“.

WinEventHandler 12 2018 04 06 14 50 52

After this is done, the “Button1” control will start working when executing the form.


It’s easy to remove and add code back in a Windows Form. Just remember that when it involves Control Event Handlers, you will need to take some extra steps to recover the event. I hope this information will be helpful when working with new and existing forms.

If you have questions about our products, please post in our support forum.
For licensed customers, use the forum associated with your product in our Product Support Forums for Registered Customers.
For users of trial versions, please post in our Former and Future Customers - Questions forum.
Copyright © 2021 SAPIEN Technologies, Inc.