Jump to content

Navigating with arrow keys on List View skips an item between rows


Go to solution Solved by Nine,

Recommended Posts

Posted

So as the title says. Let's say I have 20 items in the list view. I would like to navigate them using arrow keys, however on row's end it will jump to the first item in the next row, and vice-versa.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>

GUICreate("test", 500, 600)
Global $idListview = GUICtrlCreateListView('',  10, 10, 480, 575, _
                                     BitOR($LVS_SINGLESEL, $LVS_SHOWSELALWAYS, $LVS_NOCOLUMNHEADER, $LVS_REPORT))
_GUICtrlListView_SetView($idListview, 0)

For $i = 0 To 20
    _GUICtrlListView_AddItem($idListview, $i, $i)
    Sleep(10)
Next

GUISetState(@SW_SHOW)
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
WEnd

Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam

    Local $tNMHDR, $iIDFrom, $iCode, $iIndex
    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")

    Switch $iIDFrom
        Case $idListview
            Switch $iCode
                Case $LVN_KEYDOWN
                    Local $tInfo = DllStructCreate($tagNMLVKEYDOWN, $lParam)
                    Local $iVK = Hex(DllStructGetData($tInfo, "VKey"), 2)
                    $iIndex = _GUICtrlListView_GetSelectedIndices($idListview, True)
                    If $iIndex[0] = 0 Then Return $GUI_RUNDEFMSG
                    Switch $iVK
                        Case 25
                            ConsoleWrite($iIndex[1] & @CRLF)
                            If Mod($iIndex[1], 11) = 0 Then _GUICtrlListView_SetItemSelected($idListview, $iIndex[1] - 1, True, True)
                        Case 26
                            ConsoleWrite($iIndex[1] & @CRLF)
                        Case 27
                            ConsoleWrite($iIndex[1] & @CRLF)
                            If Mod($iIndex[1], 10) = 0 And Not $iIndex[1] = 0 Then _GUICtrlListView_SetItemSelected($idListview, $iIndex[1] + 1, True, True)
                        Case 28
                            ConsoleWrite($iIndex[1] & @CRLF)
                    EndSwitch
            EndSwitch
    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc

The problem I'm having is figuring out why it skips the first item in the next row and directly goes to the next one.

I tried various ways of getting the $iIndex however that isn't the issue. From the looks of it the moment I focus the item it "skips" (_GUICtrlListView_SetItemSelected($idListview, $iIndex[1] + 1, True, TRUE). But if I don't focus it can't continue on the next row.

  • Solution
Posted

You need to cancel default behavior of the listview, that way :

Case 25 ; left
                            ConsoleWrite("left " & $iIndex[1] & @CRLF)
                            If Mod($iIndex[1], 7) = 0 Then
                              _GUICtrlListView_SetItemSelected($idListview, $iIndex[1] - 1, True, True)
                              Return 1
                            EndIf

 

Posted
3 hours ago, Nine said:

You need to cancel default behavior of the listview, that way :

Case 25 ; left
                            ConsoleWrite("left " & $iIndex[1] & @CRLF)
                            If Mod($iIndex[1], 7) = 0 Then
                              _GUICtrlListView_SetItemSelected($idListview, $iIndex[1] - 1, True, True)
                              Return 1
                            EndIf

 

Yep that makes total sense. Somehow going through the list and not realizing it had its own default behavior didn't occur to me. No need for Mod() either now.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...