maniootek

Get Index of clicked ListView item

4 posts in this topic

#1 ·  Posted (edited)

What would be a best method to reduce CPU/memory usage to read index of clicked item from ListView?

I have modified example script from help file and my current code is:

#include <GUIConstantsEx.au3> ;$GUI_EVENT_CLOSE
#include <GuiListView.au3>
#include <WindowsConstants.au3> ;$WM_NOTIFY

Global $g_hListView

Example()

Func Example()
    Local $hGUI, $hImage
    $hGUI = GUICreate("(UDF Created) ListView Create", 400, 300)

    $g_hListView = _GUICtrlListView_Create($hGUI, "", 2, 2, 394, 268)
    _GUICtrlListView_SetExtendedListViewStyle($g_hListView, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES))
    GUISetState(@SW_SHOW)

    GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

    ; Add columns
    _GUICtrlListView_InsertColumn($g_hListView, 0, "Column 1", 100)
    _GUICtrlListView_InsertColumn($g_hListView, 1, "Column 2", 100)
    _GUICtrlListView_InsertColumn($g_hListView, 2, "Column 3", 100)

    ; Add items
    _GUICtrlListView_AddItem($g_hListView, "Row 1: Col 1")
    _GUICtrlListView_AddSubItem($g_hListView, 0, "Row 1: Col 2", 1)
    _GUICtrlListView_AddSubItem($g_hListView, 0, "Row 1: Col 3", 2)
    _GUICtrlListView_AddItem($g_hListView, "Row 2: Col 1")
    _GUICtrlListView_AddSubItem($g_hListView, 1, "Row 2: Col 2", 1)
    _GUICtrlListView_AddItem($g_hListView, "Row 3: Col 1")

    ; Loop until the user exits.
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
    GUIDelete()
EndFunc   ;==>Example

Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView, $tInfo
    ; Local $tBuffer
    $hWndListView = $g_hListView
    If Not IsHWnd($g_hListView) Then $hWndListView = GUICtrlGetHandle($g_hListView)

    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hWndListView
            Switch $iCode
                Case $NM_CLICK ; Sent by a list-view control when the user clicks an item with the left mouse button
                    $tInfo = DllStructCreate($tagNMITEMACTIVATE, $lParam)
                    $index = DllStructGetData($tInfo, "Index")
                    MsgBox(0,0,$index)
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

other method is to check in a loop if new item was selected via _GUICtrlListView_GetItemSelected()

#include <GUIConstantsEx.au3> ;$GUI_EVENT_CLOSE
#include <GuiListView.au3>

Example()

Func Example()
    Local $idListview
    Local $last_selected_item_index = -1

    GUICreate("ListView Get Item Selected", 400, 300)
    $idListview = GUICtrlCreateListView("", 2, 2, 394, 268)
    GUISetState(@SW_SHOW)

    ; Add columns
    _GUICtrlListView_AddColumn($idListview, "Items", 100)

    ; Add items
    _GUICtrlListView_AddItem($idListview, "Item 1")
    _GUICtrlListView_AddItem($idListview, "Item 2")
    _GUICtrlListView_AddItem($idListview, "Item 3")

    ; Loop until the user exits.
    Do
        $iCount = _GUICtrlListView_GetItemCount($idListview)
        For $i = 0 To $iCount - 1
            If _GUICtrlListView_GetItemSelected($idListview, $i) Then
                $now_selected_item_index = $i
                If $last_selected_item_index <> $now_selected_item_index Then
                    MsgBox(0, 0, $now_selected_item_index)
                    $last_selected_item_index = $now_selected_item_index
                    ContinueLoop
                EndIf
            EndIf
        Next
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
    GUIDelete()
EndFunc   ;==>Example

which one is better?

Edited by maniootek

Share this post


Link to post
Share on other sites



The first one.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Ok thanks for answer. Anyway, I have tried to add _GUITreeViewEx_RegMsg() from @Melba23 TreeViewEx UDF together with my first example byt unfortunatelly they don't work together (only one which was defined in code as the last one). I am new with this type of "GUIRegisterMsg" and I don't know why only one working at time. Is there some limitation to use only one RegisterMsg?

First example also crash my _ArrayDisplay function so I am staying away of this solution. Second example cause no erorrs.

Edited by maniootek

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

13 hours ago, maniootek said:

Is there some limitation to use only one RegisterMsg?

AFAIK you can only register a MSG ID to a single function...however you could write a notify function that would handle all of the controls/events you need.  


How I might approach it would be to copy the notification function from the GUITreeViewEX UDF (_GUITreeViewEx_WM_NOTIFY_Handler) to a new Notify function (i.e. _WM_NOTIFY), register the event yourself and add the functionality you need to it.

Just for an example, in this script I made I use a custom _WM_NOTIFY function to handle multiple controls on different GUIs.

Edited by spudw2k

Share this post


Link to post
Share on other sites

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