Results 1 to 13 of 13

Thread: Selecting an item in a listbox

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2004
    Posts
    21

    Selecting an item in a listbox

    Hi All,

    I have a listbox with a popup menu when I right-click on one of the names in the list. This all works fine, but in order to make this work properly I have to first left click on the name I want to select, then I can right click to bring up the popup menu.

    I'm wondering if there is a way I can just right click directly onto my chosen name in the listbox without first having to left click to select it?

    Anyone have any ideas?

    Thanks,

  2. #2
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Selecting an item in a listbox

    I don't think that's possible. ListBox will change .ListIndex property ONLY on the left click. Therefore, .ListIndex won't change on the right click, so you really don't have anything to select.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2004
    Posts
    21

    Re: Selecting an item in a listbox

    ok, thanks. I guess I'll just change it so the menu pops up when I left click on it.

  4. #4
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: Selecting an item in a listbox

    No, it can be done with right click too. I forgot the way, search here, you would find a way.
    Show Appreciation. Rate Posts.

  5. #5
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Selecting an item in a listbox

    here's a quick example of how you can get the Index of the item that was clicked on without it being selected. It's rough and ready but you should be able to fit it to your needs fairly simply:
    VB Code:
    1. Private Sub List1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    2.     Dim lCurrentPos As Long
    3.     Set Me.Font = List1.Font
    4.     lCurrentPos = List1.TopIndex + Y \ Me.TextHeight("A")
    5.     Debug.Print lCurrentPos
    6. End Sub

  6. #6

  7. #7
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Selecting an item in a listbox

    This also works.

    VB Code:
    1. Option Explicit
    2. Private Const LB_ITEMFROMPOINT As Long = &H1A9
    3.  
    4. Private Declare Function SendMessage Lib "User32.dll" Alias "SendMessageA" _
    5.     (ByVal hwnd As Long, ByVal wMsg As Long, _
    6.      ByVal wParam As Long, lParam As Any) As Long
    7.  
    8. Private Sub List1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
    9.    
    10.     Dim lngRetValue As Long
    11.     Dim lngIndex As Long
    12.     Dim lngCurPos As Long
    13.    
    14.     On Error Resume Next
    15.  
    16.     If Button = vbRightButton Then
    17.         lngCurPos = (ScaleX(x, vbTwips, vbPixels)) Or (ScaleY(y, vbTwips, vbPixels) * &H10000)
    18.         lngRetValue = SendMessage(List1.hwnd, LB_ITEMFROMPOINT, 0&, ByVal lngCurPos)
    19.         lngIndex = lngRetValue And &HFFFF
    20.         List1.ListIndex = lngIndex
    21.         MsgBox List1.List(List1.ListIndex)
    22.     End If
    23.     If Button = vbRightButton Then
    24.         PopupMenu mnuEdit
    25.     End If
    26.    
    27. End Sub

  8. #8
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: Selecting an item in a listbox

    This should work..
    VB Code:
    1. Private Const LB_ITEMFROMPOINT = &H1A9
    2. 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
    3.  
    4. Private Sub Form_Load()
    5. List1.AddItem "item 1"
    6. List1.AddItem "item 1"
    7. List1.AddItem "item 1"
    8. List1.AddItem "item 1"
    9. List1.AddItem "item 1"
    10. End Sub
    11.  
    12. Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    13.     Dim Index  As Long
    14.     Dim Coord  As Long
    15.  
    16.     If Button = vbRightButton Then
    17.         Coord = ((Y / Screen.TwipsPerPixelY) * &H10000) + (X / Screen.TwipsPerPixelX)
    18.        
    19.         Index = SendMessage(List1.hwnd, LB_ITEMFROMPOINT, 0&, ByVal lCoord)
    20.         If Index >= 0 Then
    21.             List1.ListIndex = Index
    22.             MsgBox "You right clicked on " & List1.Text
    23.         End If
    24.     End If
    25. End Sub
    Edit: Late!!
    Show Appreciation. Rate Posts.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Apr 2004
    Posts
    21

    Re: Selecting an item in a listbox

    Thanks everyone, seems it can be done... ;-)

  10. #10
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: Selecting an item in a listbox

    Hey Marty, could you please tell me, why do we multiply &H10000 or 65536 to the Y pixelated value.
    Show Appreciation. Rate Posts.

  11. #11

  12. #12
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: Selecting an item in a listbox

    Quote Originally Posted by MartinLiss
    Actually I have no idea, it's just a sample that I picked up but unfortunately I didn't record where (probably here) and who I got it from.
    OK, got it. the lparam needs to be divided into High- and Low-order words, here Y coord is a High order word while X coord is a low order word.

    Details, about High - Low order words
    A Long (Long Integer) takes up 32 bits of memory in VB and An word ("integer" in VB6) takes up 16 bits.

    Two words can be used instead of one Long integer.

    Some Windows API pass two word parameters to a function that normally takes one long integer.

    To seperate this long integer into two words you take the first 16 bits into one word (the "high word") and the second 16 bits into the other word (the "low word"). So when we multiple &H10000 or 65536, it makes the resultant value High-order word
    Here, Y coord plays vital role in selecting a value, therefore it has been made HO value.
    Show Appreciation. Rate Posts.

  13. #13
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Selecting an item in a listbox

    Quote Originally Posted by MartinLiss
    Actually I have no idea, it's just a sample that I picked up but unfortunately I didn't record where (probably here) and who I got it from.
    I did a little research and the credit should go to RhinoBull. See post #2 in this thread.

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