|
How to
display the item which the mouse is over
in a list box
I have had many letters which
have asked me how to you display in a
tooltip or some other means, such as a
text box, the current item's text in a
list box which the mouse pointer is
hovering over. I now have the answer
which uses the SendMessage API.
- Start A new
Standard-EXE project, form1 is
created by default.
- Add a list box
and a text box to form1.
- Open up the
code window for Form1 and type
the following
Option Explicit
Private Declare Function SendMessage Lib _
"user32" Alias "SendMessageA" (ByVal
hwnd _
As Long, ByVal wMsg As Long, ByVal wParam _
As Long, lParam As Any) As Long
Private Const LB_ITEMFROMPOINT = &H1A9
Private Sub Form_Load()
With List1
.AddItem "Visit"
.AddItem "Steve Anderson Web
Site AT"
.AddItem
"http://www.microweird.demon.co.uk"
End With
End Sub
Private Sub List1_MouseMove(Button _
As Integer, Shift As Integer, X As _
Single, Y As Single)
Dim lXPoint As Long
Dim lYPoint As Long
Dim lIndex As Long
If Button = 0 Then ' if no button was pressed
lXPoint = CLng(X /
Screen.TwipsPerPixelX)
lYPoint = CLng(Y /
Screen.TwipsPerPixelY)
With List1
' get
selected item from list
lIndex =
SendMessage(.hwnd, _
LB_ITEMFROMPOINT, 0, ByVal _
((lYPoint *
65536) + lXPoint))
' show tip or
clear last one
If (lIndex
>= 0) And _
(lIndex <=
.ListCount) Then
.ToolTipText = .List(lIndex)
Text1.Text = .List(lIndex)
Else
.ToolTipText = ""
End If
End With
End If
End Sub
Run the project(F5)
and hover your cursor over different
items in the list box and they will be
displayed in a tooltip and in Text1.
|
|
|
|