Click to See Complete Forum and Search --> : Listbox right click
chrisjk
Jan 25th, 2000, 12:41 AM
I have a book that shows exactly how to do what you are after in Access using VBA.
It is the same principle so it might work. Now all i have to do is find the book... :)
Back soon if i find it!!
Regards,
------------------
- Chris
chris.kilhams@btinternet.com
If it ain't broke - don't fix it :)
Cabal
Jan 25th, 2000, 01:46 AM
here u go
Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 2 Then
Dim AnyItem As ListItem
'HitTest returns a node object under the cursor
Set AnyItem = ListView1.HitTest(X, Y)
If Not AnyItem Is Nothing Then
Set ListView1.DropHighlight = AnyItem
ListView1.DropHighlight.Selected = True
End If
End If
Set AnyItem = Nothing
End Sub
i tested it works with vb6, tell me if it dosent work with 5. Thanks to developers code book for the idea.....
Aaron Young
Jan 25th, 2000, 02:01 AM
You can use a couple of API calls to select a ListItem with the Right Mouse Button, try this:
Private Type POINTAPI
X As Long
Y As Long
End Type
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function ScreenToClient Lib "user32" (ByVal hWnd As Long, lpPoint As POINTAPI) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long
Private Const LB_ITEMFROMPOINT = &H1A9
Private Sub Form_Load()
Dim iIndex As Integer
For iIndex = 1 To 100
List1.AddItem "Item" & iIndex
Next
End Sub
Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim tPOINT As POINTAPI
Dim iIndex As Long
If Button = vbRightButton Then
Call GetCursorPos(tPOINT)
Call ScreenToClient(List1.hWnd, tPOINT)
iIndex = SendMessage(List1.hWnd, LB_ITEMFROMPOINT, 0&, ByVal ((tPOINT.X And &HFF) Or (&H10000 * (tPOINT.Y And &HFF))))
If iIndex > -1 Then
iIndex = iIndex And &HFF
List1.Selected(iIndex) = True
End If
End If
End Sub
Private Sub List1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
'Do the Same as in the MouseDown Event
Call List1_MouseDown(Button, -1, X, Y)
End Sub
Private Sub List1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbRightButton Then PopupMenu mnuPopup, vbPopupMenuRightButton
End Sub
Private Sub mnuRemove_Click()
MsgBox "Remove List Item " & List1
End Sub
------------------
Aaron Young
Analyst Programmer
aarony@redwingsoftware.com
ajyoung@pressenter.com
Andy Collyer
Jan 25th, 2000, 11:40 AM
Hi folks.
When I rightclick on a listbox, I've got a menu popup. But what the menupopup displays depends on the selected item. How can I get the listbox to select the item like a leftclick before I do my menupopup stuff? As it is at the moment, the listbox doesn't select the rightclicked item at all.
Any ideas?
Thanks very much,
AndyC
------------------
* * * * * * * * * * * * * * * * * * * * * *
AndyC
London
email: andy.collyer@bigfoot.com
* * * * * * * * * * * * * * * * * * * * * *
VBfreak
Jan 25th, 2000, 11:52 AM
I don't think there is a way, unless you could invert the mouse buttons when you are over the list and to programmaticaly call the popup menu. That way the user would have the illusion that he has done a left click. GOOD LUCK :)
Andy Collyer
Jan 25th, 2000, 04:25 PM
Thanks for the replies...
Cabal - Unfortunately it's a listbox not a listview and there's a lot of code already associated with it so moving it to a listview might be more hassle than it's worth, unless there is simply no way of doing it with a listbox :)
Aaron - thanks, I'd tried that already but whenever I try to right click when an item hasn't already been selected I get an application error :( and it all closes down, which is why I assumed I was barking up the wrong tree.
Never mind, how about using the MouseMove event to select whichever item the mouse is over, then? Is there a general algorithm for working out which listitem an (X, Y) co-ordinate is at?
Thanks again,
AndyC
------------------
* * * * * * * * * * * * * * * * * * * * * *
AndyC
London
email: andy.collyer@bigfoot.com
* * * * * * * * * * * * * * * * * * * * * *
Andy Collyer
Jan 25th, 2000, 05:37 PM
Right, for those interested, here's some code to do the job...
Private Sub ListBox1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If (Button = vbRightButton) Then
' ensure the correct row is selected
Dim Item2Select As Integer
Item2Select = GetSelectedItem(ListBox1, Y)
If Item2Select >= 0 Then
ListBox1.ListIndex = Item2Select
PopupMenu menuList, , , , menuListPing
End If
End If
End Sub
' and the actual function...
Private Function GetSelectedItem(ByVal LBpassed As ListBox, ByVal Ypos As Single) As Integer
Dim h As Integer
h = TextHeight("1234567890.")
GetSelectedItem = Int(Ypos / h) + LBpassed.TopIndex
If GetSelectedItem >= LBpassed.ListCount Then GetSelectedItem = -1
End Function
AndyC
------------------
* * * * * * * * * * * * * * * * * * * * * *
AndyC
London
email: andy.collyer@bigfoot.com
* * * * * * * * * * * * * * * * * * * * * *
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.