Basics: How to add an Event Handler in PowerShell Studio

As a follow-up to our previous events blog, Basics: What is an event and how do I handle them?, this article will be about the basics of handling events in PowerShell Studio’s Designer. As previously stated, an event is a special kind of method that occurs or is raised when something happens to an object, like when you click a button, resize a window, or load a form.

PowerShell Studio’s PSF file is split into two parts: Designer and Script. The Designer tab is where the UI of the form is designed and the Script tab is where the code is written. To connect a control and an event handler, both the Designer and the Script need to be in sync.

Adding an event handler

With a PSF file open and the Designer tab selected, select the desired control. A selected control will show as outlined with white resize boxes, and some controls will have a small box with an arrow near the top-right corner:

The top-right box allows you to set specific properties and tasks for the control; the properties and tasks will vary depending on the type of control selected.

When the control is selected, navigate over to the Properties pane, which shows the selected control’s properties and events. To see events, select the Events tab:

You can also view the control’s events by right-clicking the control and selecting Add Events (Ctrl + E):

This opens a dialog listing all events for that control:

When an event has been added, you will be redirected to the function in the script:

Hovering over the generated event will tell you how many controls are referencing this script block. More than one control can use the same script block.

This script block is where you define what you want to happen once this event has been triggered.

All event handlers are listed in the Functions pane, per file:

Clicking on any of the listed event handlers will navigate you to the position in the script.

Important: If the event script block is not listed and it does not show any references to any controls, it means it is not being used by any controls. This can be resolved by opening the designer tab, selecting the control, then setting the event’s script block via the Properties Pane:

Feedback

As always, any feedback is appreciated. If you have a particular type of blog article or product feature request you would like to see, please submit your suggestions on the Wish List and Feature Requests forum or the new Feature Requests page.