Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Set ListIndex using right click vs. left click 1

Status
Not open for further replies.

JPMorgan

Programmer
Dec 10, 2001
25
US
Using VB6, i've got a ListBox containing 10 items. When you left mouse click on any item, that item is highlighted and the ListIndex is set to that item that you clicked on. I need help doing the same action for a right mouse click.

Any ideas?

My ultimate goal is to launch "frmSelection" by rightclicking on the listbox on "frmMaster". frmSelection is a smaller form with only a single listbox on it and whos top and left coords match the mouses x and y coords when the left click event happens on "frmMaster". It acts as kind of a tool tip selection list. This is all working, but I must pass the list index from "frmMaster" to "frmSelection". I cannot send the listindex if it is not selected by the right mouse click.

Thanks.
 
Try this:

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 List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim P As Long
Dim XPosition As Long, YPosition As Long
XPosition = CLng(X / Screen.TwipsPerPixelX)
YPosition = CLng(Y / Screen.TwipsPerPixelY)
P = SendMessage(List1.hWnd, LB_ITEMFROMPOINT, 0, ByVal ((YPosition * 65536) + XPosition))
If P < List1.ListCount Then
List1.ListIndex = P
End If
End Sub
 
Try this code.
(It is assumed that the font of the listbox is same as that of the container form.)
___
[tt]
Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim Idx As Long
Idx = List1.TopIndex + Y \ TextHeight(&quot;&quot;)
If Idx < List1.ListCount Then List1.ListIndex = Idx
End Sub
[/tt]
 
Thanks a lot, to you both. I ended up using Hypetia's suggestion and it works well.

Again, Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top