VBA: Right-Clicking in ListBoxes

M

Mickey F.

Is there a way to get Word to return the Index of an item in a ListBox that
you right-clicked over?

This is for Word 2002.
 
K

Karl E. Peterson

Mickey F. said:
Is there a way to get Word to return the Index of an item in a ListBox that
you right-clicked over?

This is for Word 2002.

Well, you'll need to obtain its hWnd (GetFocus, maybe?). Once you have that, you
can translate mouse position to ListIndex something like this:

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_GETITEMHEIGHT = &H1A1

Private Sub List1_MouseMove(Button As Integer, _
Shift As Integer, X As Single, Y As Single)
Dim ItemHeight As Long
Dim NewIndex As Long
Static OldIndex As Long

With List1
ItemHeight = SendMessage(.hWnd, _
LB_GETITEMHEIGHT, 0, ByVal 0&)
ItemHeight = .Parent.ScaleY(ItemHeight, _
vbPixels, vbTwips)
NewIndex = .TopIndex + (Y \ ItemHeight)
If NewIndex <> OldIndex Then
If NewIndex < .ListCount Then
.ToolTipText = .List(NewIndex)
Else
.ToolTipText = vbNullString
End If
OldIndex = NewIndex
End If
End With
End Sub

Source:

Ask the VB Pro, June 2000
http://vb.mvps.org/articles/ap200006.asp

That was originally written for Classic VB, of course, but the same general
principles apply. In this case, you're only after the value NewIndex.
 
P

Perry

No designated Rightclick event.
But however, you can mimick it, using the MouseUp (or Down) event
and catching the Button callback:
1 is the left- and 2 is the right mousebutton.

Private Sub ListBox1_MouseUp( _
ByVal Button As Integer, _
ByVal Shift As Integer, _
ByVal X As Single, _
ByVal Y As Single)

If Button = 2 Then
If Me.ListBox1.ListIndex > -1 Then
MsgBox Me.ListBox1.ListIndex
Else
MsgBox "No Selection"
End If
End If
End Sub

Krgrds,
Perry
 
P

Perry

No designated Rightclick event.
But however, you can mimick it, using the MouseUp (or Down) event
and catching the Button callback:
1 is the left- and 2 is the right mousebutton.

Private Sub ListBox1_MouseUp( _
ByVal Button As Integer, _
ByVal Shift As Integer, _
ByVal X As Single, _
ByVal Y As Single)

If Button = 2 Then
If Me.ListBox1.ListIndex > -1 Then
MsgBox Me.ListBox1.ListIndex
Else
MsgBox "No Selection"
End If
End If
End Sub

Krgrds,
Perry
 
P

Perry

No designated Rightclick event.
But however, you can mimick it, using the MouseUp (or Down) event
and catching the Button callback:
1 is the left- and 2 is the right mousebutton.

Private Sub ListBox1_MouseUp( _
ByVal Button As Integer, _
ByVal Shift As Integer, _
ByVal X As Single, _
ByVal Y As Single)

If Button = 2 Then
If Me.ListBox1.ListIndex > -1 Then
MsgBox Me.ListBox1.ListIndex
Else
MsgBox "No Selection"
End If
End If
End Sub

Krgrds,
Perry
 
M

Mickey F.

That doesn't seem to work for what I'm looking for. I was hoping to return
the Index value of the item the mouse was currently over when I
right-clicked. But since right-clicking doesn't change the ListIndex, it
just keeps message boxing whatever ListIndex currently is, rather than for
the item I right-clicked over.
 
P

Perry

Sorry for multiple posts :)
My newsreader jammed and I had to "push" the answers, resulting in ... well
sorry for that.
Another sorry for not understanding yr question ...

Have you read the other contribution to this thread?
Worth looking at.

Imo, a little "user education" will help as well.
Selecting an item in the listbox, and "dragging" the selection across the
listitems will change
the listindex.
Do you really want the listindex to change on mouse-hovering ??

Krgrds,
Perry
 
M

Mickey F.

To be honest, I'm a little unsure of what's going on in that Private Declare
Function statement. I've never really used them. What it looks like is that
your using a function from the user32 library. I've never used anything like
that before, so I'm a bit lost there.

I guess what I'm really looking for is an explanation of what SendMessage
does. The article that you linked didn't go into detail on it. I don't know
what any of the parameters are and not sure where to find documentation on
it, and (if my suppositions are correct) any other pre-made functions like it.

Also, another novice questions here, what significance is the Alias
"SendMessageA" portion?
 
M

Mickey F.

Imo, a little "user education" will help as well.
Man I wish I could educate my users. There will probably be 100+ people
using this and many of them resist technology like it was the end of all
things good and pure in the world.
Have you read the other contribution to this thread?
Worth looking at.
I have, but I'm not on the same level as the person who contributed it. I'm
not understanding the Declare Function statement and how it can be used so
I'm researching that a bit.
Selecting an item in the listbox, and "dragging" the selection across the
listitems will change
the listindex.
Do you really want the listindex to change on mouse-hovering ??
Not on MouseMove, but if I combine your Mouse_Up event suggestion, (after I
figure out how to use this Declare Function stuff) with Karl's suggestion
then I should get what I want.
Sorry for multiple posts :)
My newsreader jammed and I had to "push" the answers, resulting in ... well
sorry for that.
Another sorry for not understanding yr question ...
No problem at all! I'm sorry for my vague wording.

Thanks for all the help!
 
K

Karl E. Peterson

Hi Mickey --
To be honest, I'm a little unsure of what's going on in that Private Declare
Function statement. I've never really used them. What it looks like is that
your using a function from the user32 library. I've never used anything like
that before, so I'm a bit lost there.

Yeah, SendMessage is a very powerful tool provided by the Windows API. You "send" a
message to any particular window, and it (hopefully) responds with some information
you want/need.
I guess what I'm really looking for is an explanation of what SendMessage
does. The article that you linked didn't go into detail on it. I don't know
what any of the parameters are and not sure where to find documentation on
it, and (if my suppositions are correct) any other pre-made functions like it.

Okay, sorry about that. Lots of implied knowledge there. If you want to learn
about that sort of thing, I'd recommend starting slow, with specific examples (like
this, in fact) or at sites like http://word.mvps.org
Also, another novice questions here, what significance is the Alias
"SendMessageA" portion?

Ah, most of the API calls that work with strings in Windows come in both ANSI (A)
and Unicode (W) flavors. VB automatically converts strings to ANSI when passing to
external DLLs, so that's the one we need to use most often.

Sadly, that all said, I just tried my suggestion in Word, and it doesn't work.
Seems the listbox in Word isn't a "real" listbox afterall. It doesn't respond to
the LB_* messages. :-(

Sorry... Karl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top