Results 1 to 7 of 7

Thread: Listbox right click

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923

    Post

    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

  2. #2
    New Member
    Join Date
    Jan 2000
    Posts
    5

    Post

    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.....

  3. #3
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Post

    You can use a couple of API calls to select a ListItem with the Right Mouse Button, try this:
    Code:
    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


  4. #4
    Member
    Join Date
    Jan 1999
    Location
    London, UK
    Posts
    58

    Post

    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
    * * * * * * * * * * * * * * * * * * * * * *

  5. #5
    New Member
    Join Date
    Jan 2000
    Location
    canada
    Posts
    15

    Post

    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

  6. #6
    Member
    Join Date
    Jan 1999
    Location
    London, UK
    Posts
    58

    Post

    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
    * * * * * * * * * * * * * * * * * * * * * *

  7. #7
    Member
    Join Date
    Jan 1999
    Location
    London, UK
    Posts
    58

    Post

    Right, for those interested, here's some code to do the job...

    Code:
    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
    * * * * * * * * * * * * * * * * * * * * * *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width