Fun with PowerShell: Add ToolTips to a Windows Form
May 26, 2010
posted by James Kehr
While working on a new scripting project I came across a need to add a tool tip to a button. As all good scripters do I started by searching the internet to see is anyone had done the work for me; alas, the only person I found who had successfully added a ToolTip to a PowerShell form had cheated by using Visual Studio - I didn't even know Visual Studio had PowerShell editing capabilities - so off I went in search of a method of adding ToolTips to PowerShell.
http://msdn.microsoft.com/en-us/library/system.windows.forms.tooltip.aspx
ToolTip is the .NET term for those little informational balloons that pop-up when you hover the mouse over a button or icon or whatever. They are used in far more than Windows Forms, but for the purposes of this article we'll stick to forms. There are two kinds of ToolTip popup, a balloon and an OS default.
Balloon:
Default:
Standard text rules apply for the content of the ToolTip window, which means you can use `n for a carriage return, `t for a tab, and escape characters. The only real trick to getting a ToolTip to work is attaching it to an object. The MSDN site makes it seem far more complicated than it really is and once you know the trick it is easy to do.
First, create your form and then define your ToolTip after you define the control, or form object, you want to attach the ToolTip to. To attach the ToolTip you use the SetToolTip method with the following format:
$ToolTip.SetToolTip($Button, "A ToolTip")
$Button is simply a variable that stores a visible form object defined. After that is your text, as a string or string variable. And that's it. If you want to play with the defaults you can read the link above to get details about the properties and the methods available. My sample code, included at the end, contains some tweaks that I've made for my main script. This is also the code I used to generate the images above.
ToolTips can be attached to any object in your form. Just don't ask me how to use the Show() method because I have no idea. Now, without further adieu, here is the sample code.
#tooltiptest.ps1 [void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.forms") $ScriptPath = Split-Path -Path $MyInvocation.MyCommand.Path -Parent # creates a form for input via a window $Global:objForm = New-Object System.Windows.Forms.Form $objForm.BackColor = [System.Drawing.Color]::WhiteSmoke $objForm.Icon = new-object System.Drawing.Icon("$ScriptPathavicon.ico") $objForm.Text = "A Form" $objForm.AutoSize = $True $objForm.AutoSizeMode = "GrowAndShrink" $objForm.StartPosition = "CenterScreen" $objForm.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::Fixed3D $objForm.Font = New-Object System.Drawing.Font("Verdana",8,[System.Drawing.FontStyle]::Regular) # Button: close launcher $Button = New-Object System.Windows.Forms.Button #$btnCancel.Location = New-Object System.Drawing.Size(50,180) $Button.Size = New-Object System.Drawing.Size(150,25) $Button.Font = New-Object System.Drawing.Font("Verdana",8,[System.Drawing.FontStyle]::Bold) $Button.BackColor = [System.Drawing.Color]::Salmon $Button.Text = "A Button" $Button.add_Click({;$objForm.Close();$objForm.Dispose()}) $objForm.Controls.Add($Button) #ToolTip: close launder $ToolTip = New-Object System.Windows.Forms.ToolTip $ToolTip.BackColor = [System.Drawing.Color]::LightGoldenrodYellow $ToolTip.IsBalloon = $true $ToolTip.InitialDelay = 500 $ToolTip.ReshowDelay = 500 $ToolTip.SetToolTip($Button, "A ToolTip") # Activates/draws the form. $objForm.Add_Shown({$objForm.Activate()}) [void] $objForm.ShowDialog()
#James Kehr
Get-Member $OW | ?{$_.title -eq "System Administrator"`
-and $_.certification -match "MCITP 2008, MCSE 2000, MCDST, Network+, A+"}
New-Variable -name company -value 'ORCS Web, Inc.' -description 'www.orcsweb.com | 1.888.313.9421'












