Add Pop-Up Tips for PowerShell WinForms Script

Posted on December 16, 2014 by Jeff Hicks in PowerShell with 0 Comments

IT pros who have taken the leap and dived head-long into PowerShell will eventually find themselves creating graphical PowerShell scripts and tools. This often means using Windows Forms, which is also known as WinForms. You can create some very complicated graphical tools and often IT pros do just that, where these tools are delegated for administration. I would expect the form interface to be intuitive most of the time, but perhaps you might want to add a little interactive help. You can use the ToolTip control to accomplish this.

Using a WinForms-Based PowerShell Script

Here’s a simple WinForms-based PowerShell script:

The script will present a simple form with a few check boxes.

PowerShell script displays a dialog box. (Image Credit: Jeff Hicks)

PowerShell script displays a dialog box. (Image Credit: Jeff Hicks)

Click, OK and a few WMI queries will run, presenting the results in a new form.

WMI results. (Image Credit: Jeff Hicks)

WMI results. (Image Credit: Jeff Hicks)

This isn’t especially complicated, but let’s add some help. The way I handle this in my forms is to display the help when the user hovers the mouse over a given control. The help text is displayed in the ToolTip control. First, let’s add the control to the script.

When working with WinForms in a PowerShell script, we have to connect some PowerShell action with some event from the form, such as clicking a button or hovering the mouse.

Sponsored

In my projects, I define these actions in scriptblocks. I’m going to write a scriptblock to display the pop-up help for selected controls.

The scriptblock includes a switch statement that looks at a string and depending on what it matches, in this case text1 or check2, it assigns a value to the variable $tip and applies the value to the ToolTip object. The $this refers to active control. For each control in my form, I’ll add a connection for the _MouseHover event.

When the form detects that event, it will run the associated command, which in my script is the $ShowHelp scriptblock. If the user hovers on the $Text1 control, then $this will be that control. You must make sure you give each control a name in order for the switch statement to work. The control name doesn’t have to match the variable name, but mine just happens to. I can repeat this process for the other controls in my form. Here is my revised script:

Now when the user hovers the mouse over one the defined controls, the Tooltip control displays my defined help message.

The form now displays a help message. (Image Credit: Jeff Hicks)

The form now displays a help message. (Image Credit: Jeff Hicks)

When the mouse moves away, the tooltip automatically goes away. Controls without the attached event will have no popup help. I hard coded my popup help, but it wouldn’t be that difficult to read help strings from an external source. This would be especially helpful if you need to provide localized popup text.

Sponsored

You can do all of this manually for simple forms. However, IT pros rarely take the simple approach. A popular tool for creating WinForm-based PowerShell scripts is a commercial tool from SAPIEN called PowerShell Studio. Because this product is widely used, I’ll demonstrate how to add tool tips to a PowerShell Studio project in a future article.

Sponsored

Tagged with ,