Bind a tooltip to listbox elements

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.
This topic is 8 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
owinsloe
Posts: 161
Last visit: Mon Apr 01, 2024 3:01 pm
Been upvoted: 1 time

Bind a tooltip to listbox elements

Post by owinsloe »

I started messing around with this some time back and thought I would post the working solution here as it looks like others have wanted to do this (There may be later and better solutions but this works fine for me).

First create a MouseMove event on your listbox (my example is on $listbox6) and create code similar to the following.

It's important that Tootip tip object has a global or script scope or the results are a bit awful.

You can mess around with other tooltip properties with delays but I find what's here to be fine.

$listbox6_MouseMove=[System.Windows.Forms.MouseEventHandler]{
$Index = $listbox6.IndexFromPoint($_.X, $_.Y)

# negative index when listbox is empty
if ( $Index -lt 0 ) { return }

$HoverDelay = ( (get-date)-$script:ListboxHoverTime).totalseconds
# Only invoke tooltip if hover delay exceeded or the element has changed
if (($Index -ne $script:ListboxhoverIndex) -or ($HoverDelay -ge 2))
{
# Create the tooltip values
$Item = $ListBox6.Items[$Index]
$Element = $Item.ToString()

$BalloonText = "$Element`n"
#$BalloonText += "Other stuff`n"


if ($script:ToolTip -eq $null)
{
$script:ToolTip = New-Object System.Windows.Forms.ToolTip
}
$ToolTip.InitialDelay = 1000
$ToolTip.SetToolTip($ListBox6, "$balloonText")
}
[int]$script:ListboxhoverIndex=$Index
$script:ListboxHoverTime=get-date
}
User avatar
SAPIEN Support Forums
Posts: 945
Last visit: Thu Oct 22, 2015 1:10 pm

Bind a tooltip to listbox elements

Post by SAPIEN Support Forums »

This is an automated post. A real person will respond soon.

Thank you for posting, owinsloe.

Here are some hints to help you get an accurate and complete answer to your question.

Ask in the best forum: If you asked in the wrong forum, just copy your question to the right forum.

Anticipate follow-up questions!

Did you remember to include the following?
  • 1. Product, version and build
    2. 32 or 64 bit product
    3. Operating system, e.g. Windows 7 64 bit.
    4. Attach a screenshot, if applicable
    5. Attach logs, crash reports, etc., in a ZIP file
If not, please take a moment to edit your original post or reply to this one.

*** Make sure you do not post any licensing information ***
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Bind a tooltip to listbox elements

Post by davidc »

[POST MOVED BY MODERATOR]

You can add a ToolTip control in the designer so that don't have to create it in the script.

The tooltip itself will handle the hovering.

So in the end all you need is:
PowerShell Code
Double-click the code block to select all.
$listbox1_MouseMove=[System.Windows.Forms.MouseEventHandler]{
	#Event Argument: $_ = [System.Windows.Forms.MouseEventArgs]
	#TODO: Place custom script here
	$index = $listbox1.IndexFromPoint($_.Location)
	
	if ($index -ne -1)
	{ $tooltip1.SetToolTip($listbox1, $listbox1.Items[$index].ToString()) }
	else
	{ $tooltip1.SetToolTip($listbox1, "") }
}
You might want to add a script scope variable tracking if the index has changed so that it doesn't keep assigning the same value while you move the mouse on the same list item.

David
David
SAPIEN Technologies, Inc.
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Bind a tooltip to listbox elements

Post by davidc »

For reference, here is the link to the Spotlight article on the ToolTip control:

https://www.sapien.com/blog/2014/01/30/ ... p-control/

David
David
SAPIEN Technologies, Inc.
This topic is 8 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked