dcsimg
Results 1 to 4 of 4

Thread: [RESOLVED] Set keyboard focus on ListView

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    2,443

    Resolved [RESOLVED] Set keyboard focus on ListView

    I'm trying to set focus back on the ListView in my shell browser control, but no matter what I do pressing an arrow key moves the focus to another control (besides, of course, actually physically clicking on it with the mouse).

    Even sending LBUTTONDOWN/LBUTTONUP to click on an item doesn't work; visually it restores focus, including the black focus rectangle, but a navigation key press moves focus to another control.

    Here's everything that has been tried so far:
    Code:
    SetFocusAPI UserControl.hWnd
    SetFocusAPI hLVS
    'SendMessage hLVS, WM_SETFOCUS, 0&, ByVal 0&
    Dim nSel As Long
    nSel = ListView_GetSelectedItem(hLVS)
    ListView_SetItemState hLVS, nSel, LVIS_FOCUSED Or LVIS_SELECTED, LVIS_FOCUSED Or LVIS_SELECTED
    Dim rct As oleexp.RECT
    ListView_GetItemRect hLVS, nSel, rct, LVIR_ICON
    Dim lpr As Long
    lpr = PackWords(rct.Left + 1, rct.Top + 1)
    
    SendMessage hLVS, WM_LBUTTONDOWN, 0&, ByVal lpr
    SendMessage hLVS, WM_LBUTTONUP, 0&, ByVal 0&

  2. #2
    PowerPoster
    Join Date
    Feb 2017
    Posts
    2,103

    Re: Set keyboard focus on ListView

    This is probably because something is setting the focus to the other control after you set it to the listview.

    I would try:
    Code:
    PostMessage hLVS, WM_SETFOCUS...
    or to set an API timer and to set the focus in the timer event.

  3. #3
    PowerPoster
    Join Date
    Feb 2017
    Posts
    2,103

    Re: Set keyboard focus on ListView

    I don't mean it as the solution, but did you try what happens adding a DoEvents before the SetFocusAPI?

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    2,443

    Re: Set keyboard focus on ListView

    Just DoEvents didn't help, but yeah something must have been somehow stealing the focus without stealing the focus rectangle, because calling it via timer worked. Thanks!

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width