Changing tooltip text for multiple listbox entries

By | 2002-04-19

The purpose of this code is to display a tooltip giving a description of each entry in a listbox.

Original Author: William A. Henderson I

Inputs

Standard inputs relative to the MoveMouse event.

Assumptions

Familiarity with the ItemData property of the listbox control.

Code

'FACED WITH THE PROBLEM OF SHOWING OFFICE CODES
'FOR OFFICES WITH DUPLICATE NAMES IN A LISTBOX,
'AND NOT WANTING TO INCUDE THE NUMBER IN THE
'TEXT ENTRY IN THE LISTBOX, I DEVELOPED A QUICK
'WAY OF SHOWING THE NUMBER WHICH WAS STORED IN
'THE LISTBOX ITEMDATA PROPERTY.
'
'NOTE:
'WordHeight = 195 (depending on the font used).
'
'THIS CODE IS AN IMPROVEMENT UPON CODE PREVIOUSLY
'SUBMITTED BY ANOTHER VB PROGRAMMER INWHICH THE
'PROGRAMMER LOOPED THROUGH EVERY ITEM IN THE
'LISTBOX TO DETERMINE WHICH TEXT TO DISPLAY IN THE
'TOOLTIP. THE PROBLEM ENCOUNTERED BY THAT CODE WAS
'THAT IT DID NOT WORK FOR LARGE LISTBOXES WITH
'ENTRIES GREATER THAN 167. ON THE 168th ENTRY, AN
'OVERFLOW ERROR WAS ENCOUNTERED. MY CODE IS FASTER
'AND TAKES YOU DIRECTLY TO THE ENTRY WITHOUT
'LOOPING THROUGH THE LIST.
'
Private Sub ListBox1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  Dim index As Integer
  
  index = ListBox1.TopIndex + ((Y) / WordHeight)
  ListBox1.ToolTipText = Str(ListBox1.ItemData(index))
  
End Sub

Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.