Results 1 to 11 of 11

Thread: [RESOLVED] Highlight a listbox entry-NO click event

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    28

    Resolved [RESOLVED] Highlight a listbox entry-NO click event

    Hello,

    I have a listbox, and I am using a SendMessage API call in the MouseMove event in order to get the index of the list item over which the cursor is hovering. I then want to highlight that item in the list, but WITHOUT sending a click event. Currently, I use the folllowing code...

    VB Code:
    1. List1.Selected(index) = true

    Whereas, that does highlight the listbox entry (index), it also sends a click event, which i do not want to do until the end user actually clicks the mouse.

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Highlight a listbox entry-NO click event

    I don't think what you're after is possible to achieve. You might have to consider moving your selection handler logic to the MouseUp event instead (I hope that exists).

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    28

    Re: Highlight a listbox entry-NO click event

    Penagate,

    Thanks for the reply, but let me explain further. If I have a list box with 8 items in it, as I hover down the list, I want the current selection to be highlighted, but I do not want to automatically fire off the click event just because I am hovering over the 1st item in the list. This is a very common application...actually, I'm quite frustrated that I can't figure it out, because it shouldn't be that difficult to do after you've retrieved the list index.

  4. #4
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    Re: Highlight a listbox entry-NO click event

    How about just setting a public flag that causes the click event to be ignored, like this
    VB Code:
    1. '
    2.     '   Get the index of the item under the mouse cursor
    3.     '
    4.     idx = SendMessage(LB.hwnd, LB_ITEMFROMPOINT, 0, ByVal xyPos)
    5.     '
    6.     '   Cause the list box highlight to follow the mouse
    7.     '
    8.     bIgnoreClick = True        ' Tell the list box to ignore the click event
    9.     LB.ListIndex = idx
    10.     bIgnoreClick = False
    And then in the list box's click event
    VB Code:
    1. Private Sub List1_Click()
    2.    
    3.     If bIgnoreClick Then
    4.         Exit Sub
    5.     End If
    6.    
    7. End Sub
    Not very 'techy', but it works just fine.
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Highlight a listbox entry-NO click event

    Do you actually need the item you are hovering over to be selected? Or are you simply after a highlight effect?

    If the latter is what you want, I would suggest moving to a ListView control, which provides a HotTracking (or something) property. That will give you a highlight effect without actually selecting items as you move the mouse.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    28

    Re: Highlight a listbox entry-NO click event

    pnish,

    Thanks for the reply. You're right, it's "low-tech", but it works...lol.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    28

    Re: Highlight a listbox entry-NO click event

    Penagate,

    It is only the highlight effect I want, until the user clicks the mouse; only then, do I want the click event to engage. I'll take a look at the listview control...thanks.

  8. #8
    Fanatic Member daydee's Avatar
    Join Date
    Jun 2001
    Location
    Canada
    Posts
    560

    Re: Highlight a listbox entry-NO click event

    This should not fire the ListBox's click event...
    VB Code:
    1. Private Const LB_SETSEL = &H185&
    2. Call SendMessage(ListBox.hWnd, LB_SETSEL, True, ByVal index)
    Give your music collection a whole new life with PartyTime Jukebox

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    28

    Re: Highlight a listbox entry-NO click event

    Hi Daydee,

    Thanks for the reply. It did not fire the click event, however, it dit not highlight the items in the listbox either.

  10. #10
    Fanatic Member daydee's Avatar
    Join Date
    Jun 2001
    Location
    Canada
    Posts
    560

    Re: Highlight a listbox entry-NO click event

    Ok I forgot to mention that you need to have the listBox's MultiSelect property set to Simple or Extended for this to work.

    BTW, this is exactly what I needed once too.
    Here's basically how I acheived it using a listbox with multiSelect = 1
    VB Code:
    1. Private Declare Function LBItemFromPt Lib "comctl32.dll" (ByVal hLB As Long, ByVal ptx As Integer, ByVal pty As Integer, ByVal bAutoScroll As Long) As Long
    2. Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    3. Private Type POINTAPI
    4. X As Long
    5. Y As Long
    6. End Type
    7. Private tPos As POINTAPI
    8. Private Const LB_SETSEL = &H185&
    9. Private Declare Function SendMessage Lib "user32" _
    10.         Alias "SendMessageA" _
    11.         (ByVal hWnd As Long, _
    12.         ByVal wMsg As Long, _
    13.         ByVal wParam As Long, _
    14.         lparam As Any) As Long
    15.  
    16. Private Sub List1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    17.     Dim index As Long
    18.     Call SendMessage(List1.hWnd, LB_SETSEL, False, ByVal -1)
    19.     GetCursorPos tPos
    20.     index = LBItemFromPt(List1.hWnd, tPos.X, tPos.Y, 0)
    21.     Call SendMessage(List1.hWnd, LB_SETSEL, True, ByVal index)
    22. End Sub
    Cheers!
    Give your music collection a whole new life with PartyTime Jukebox

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    28

    Re: Highlight a listbox entry-NO click event

    Daydee,

    Thanks a million...works perfectly!

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