Jump to content

ListView : Get The clicked Cell


Recommended Posts

Hi All,

I have been breaking my head over this :  When the cell it clicked , I need to get the underlying text and Row no.  but it simply doesnt happen when I click the Cell, I have to click the Header of the ListView .

Where have I messed up ? Or What have I missed ?

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

#include <MsgBoxConstants.au3>

Opt("GUIOnEventMode", 1)

Local $aItems[10][2]
For $iI = 0 To UBound($aItems) - 1
    $aItems[$iI][0] = "Row " & $iI
    $aItems[$iI][1] = "Item " & $iI
Next

$Form1 = GUICreate("Form1", 850, 830, -1, -1)
$ListView1 = GUICtrlCreateListView("Type|From ID", 13, 180, 826, 325, BitOR($GUI_SS_DEFAULT_LISTVIEW, $LVS_SINGLESEL, $WS_HSCROLL, $WS_VSCROLL, $WS_BORDER))
GUICtrlSetOnEvent(-1, "ListView1")
_GUICtrlListView_SetExtendedListViewStyle($ListView1, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_GRIDLINES, $LVS_EX_HEADERDRAGDROP))
GUISetOnEvent($GUI_EVENT_CLOSE, "Form2Close")
_GUICtrlListView_AddArray($ListView1, $aItems)

GUISetState(@SW_SHOW)

While 1
    Sleep(100)
WEnd

Func ListView1()

    ConsoleWrite('1: ' & _GUICtrlListView_GetItem($ListView1, 0) & @CRLF)
    ConsoleWrite('1: ' & _GUICtrlListView_GetItemText($ListView1, 0) & @CRLF)
    ConsoleWrite('2: ' & GUICtrlRead(GUICtrlRead($ListView1)) & @CRLF)
    ConsoleWrite('3: ' & _GUICtrlListView_GetSelectedIndices($ListView1,FALSE) & @CRLF)

EndFunc   ;==>ListView1

Func Form2Close()
    Exit
EndFunc   ;==>Form2Close

 

Thanks and regards

DR

 

[UPDATE]

Found the solution , although not elegant.

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

#include <MsgBoxConstants.au3>

Opt("GUIOnEventMode", 1)

Local $aItems[10][2]
For $iI = 0 To UBound($aItems) - 1
    $aItems[$iI][0] = "Row " & $iI
    $aItems[$iI][1] = "Item " & $iI
Next

$Form1 = GUICreate("Form1", 850, 830, -1, -1)
$ListView1 = GUICtrlCreateListView("Type|From ID", 13, 180, 826, 325, BitOR($LVS_SINGLESEL, $WS_HSCROLL, $WS_VSCROLL, $WS_BORDER))
_GUICtrlListView_SetExtendedListViewStyle($ListView1, BitOR($LVS_EX_ONECLICKACTIVATE, $LVS_EX_TRACKSELECT, $LVS_EX_FULLROWSELECT, $LVS_EX_GRIDLINES, $LVS_EX_HEADERDRAGDROP))
GUISetOnEvent($GUI_EVENT_CLOSE, "Form2Close")
_GUICtrlListView_AddArray($ListView1, $aItems)

GUISetState()
GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY")

GUISetState(@SW_SHOW)

While 1
    Sleep(100)
WEnd

Func Form2Close()
    Exit
EndFunc   ;==>Form2Close

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

    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hWndListView
            Switch $iCode
                Case $LVN_COLUMNCLICK ; A column was clicked
                    $tInfo = DllStructCreate($tagNMLISTVIEW, $lParam)
                    _DebugPrint("$LVN_COLUMNCLICK" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode & @CRLF & _
                            "-->Item:" & @TAB & DllStructGetData($tInfo, "Item") & @CRLF & _
                            "-->SubItem:" & @TAB & DllStructGetData($tInfo, "SubItem") & @CRLF & _
                            "-->NewState:" & @TAB & DllStructGetData($tInfo, "NewState") & @CRLF & _
                            "-->OldState:" & @TAB & DllStructGetData($tInfo, "OldState") & @CRLF & _
                            "-->Changed:" & @TAB & DllStructGetData($tInfo, "Changed") & @CRLF & _
                            "-->ActionX:" & @TAB & DllStructGetData($tInfo, "ActionX") & @CRLF & _
                            "-->ActionY:" & @TAB & DllStructGetData($tInfo, "ActionY") & @CRLF & _
                            "-->Param:" & @TAB & DllStructGetData($tInfo, "Param"))
                    ; No return value
                Case $LVN_DELETEITEM ; An item is about to be deleted
                    $tInfo = DllStructCreate($tagNMLISTVIEW, $lParam)
                    _DebugPrint("$LVN_DELETEITEM" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode & @CRLF & _
                            "-->Item:" & @TAB & DllStructGetData($tInfo, "Item") & @CRLF & _
                            "-->SubItem:" & @TAB & DllStructGetData($tInfo, "SubItem") & @CRLF & _
                            "-->NewState:" & @TAB & DllStructGetData($tInfo, "NewState") & @CRLF & _
                            "-->OldState:" & @TAB & DllStructGetData($tInfo, "OldState") & @CRLF & _
                            "-->Changed:" & @TAB & DllStructGetData($tInfo, "Changed") & @CRLF & _
                            "-->ActionX:" & @TAB & DllStructGetData($tInfo, "ActionX") & @CRLF & _
                            "-->ActionY:" & @TAB & DllStructGetData($tInfo, "ActionY") & @CRLF & _
                            "-->Param:" & @TAB & DllStructGetData($tInfo, "Param"))
                    ; No return value
                Case $LVN_HOTTRACK ; Sent by a list-view control when the user moves the mouse over an item
                    $tInfo = DllStructCreate($tagNMLISTVIEW, $lParam)
                    ListView_HOTTRACK(DllStructGetData($tInfo, "SubItem"))
                    ; _DebugPrint("$LVN_HOTTRACK" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                    ; "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                    ; "-->Code:" & @TAB & $iCode & @CRLF & _
                    ; "-->Item:" & @TAB & DllStructGetData($tInfo, "Item") & @CRLF & _
                    ; "-->SubItem:" & @TAB & DllStructGetData($tInfo, "SubItem") & @CRLF & _
                    ; "-->NewState:" & @TAB & DllStructGetData($tInfo, "NewState") & @CRLF & _
                    ; "-->OldState:" & @TAB & DllStructGetData($tInfo, "OldState") & @CRLF & _
                    ; "-->Changed:" & @TAB & DllStructGetData($tInfo, "Changed") & @CRLF & _
                    ; "-->ActionX:" & @TAB & DllStructGetData($tInfo, "ActionX") & @CRLF & _
                    ; "-->ActionY:" & @TAB & DllStructGetData($tInfo, "ActionY") & @CRLF & _
                    ; "-->Param:" & @TAB & DllStructGetData($tInfo, "Param"))
                    Return 0 ; allow the list view to perform its normal track select processing.
                    ;Return 1 ; the item will not be selected.
                Case $LVN_KEYDOWN ; A key has been pressed
                    $tInfo = DllStructCreate($tagNMLVKEYDOWN, $lParam)
                    _DebugPrint("$LVN_KEYDOWN" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode & @CRLF & _
                            "-->VKey:" & @TAB & DllStructGetData($tInfo, "VKey") & @CRLF & _
                            "-->Flags:" & @TAB & DllStructGetData($tInfo, "Flags"))
                    ; No return value
                Case $NM_CLICK ; Sent by a list-view control when the user clicks an item with the left mouse button
                    $tInfo = DllStructCreate($tagNMITEMACTIVATE, $lParam)
;~                  ConsoleWrite(">>>>>>>>>>> " & _GUICtrlListView_GetSelectedIndices($ListView1) & @CRLF)
                    Local $aListItem = _GUICtrlListView_GetItemTextArray($ListView1, -1)
;~                  ConsoleWrite(">>>>>>>>>>> " & _ArrayToString($aListItem) & @CRLF)
                    ConsoleWrite(">>>>>>>>>>> " & $aListItem[DllStructGetData($tInfo, "SubItem") + 1] & @CRLF)

;~                  _GUICtrlListView_GetItemTextString()
;~                  _DebugPrint("$NM_CLICK" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
;~                          "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
;~                          "-->Code:" & @TAB & $iCode & @CRLF & _
;~                          "-->Index:" & @TAB & DllStructGetData($tInfo, "Index") & @CRLF & _
;~                          "-->SubItem:" & @TAB & DllStructGetData($tInfo, "SubItem") & @CRLF & _
;~                          "-->NewState:" & @TAB & DllStructGetData($tInfo, "NewState") & @CRLF & _
;~                          "-->OldState:" & @TAB & DllStructGetData($tInfo, "OldState") & @CRLF & _
;~                          "-->Changed:" & @TAB & DllStructGetData($tInfo, "Changed") & @CRLF & _
;~                          "-->ActionX:" & @TAB & DllStructGetData($tInfo, "ActionX") & @CRLF & _
;~                          "-->ActionY:" & @TAB & DllStructGetData($tInfo, "ActionY") & @CRLF & _
;~                          "-->lParam:" & @TAB & DllStructGetData($tInfo, "lParam") & @CRLF & _
;~                          "-->KeyFlags:" & @TAB & DllStructGetData($tInfo, "KeyFlags"))
                    ; No return value
                Case $NM_DBLCLK ; Sent by a list-view control when the user double-clicks an item with the left mouse button
                    $tInfo = DllStructCreate($tagNMITEMACTIVATE, $lParam)
                    _DebugPrint("$NM_DBLCLK" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode & @CRLF & _
                            "-->Index:" & @TAB & DllStructGetData($tInfo, "Index") & @CRLF & _
                            "-->SubItem:" & @TAB & DllStructGetData($tInfo, "SubItem") & @CRLF & _
                            "-->NewState:" & @TAB & DllStructGetData($tInfo, "NewState") & @CRLF & _
                            "-->OldState:" & @TAB & DllStructGetData($tInfo, "OldState") & @CRLF & _
                            "-->Changed:" & @TAB & DllStructGetData($tInfo, "Changed") & @CRLF & _
                            "-->ActionX:" & @TAB & DllStructGetData($tInfo, "ActionX") & @CRLF & _
                            "-->ActionY:" & @TAB & DllStructGetData($tInfo, "ActionY") & @CRLF & _
                            "-->lParam:" & @TAB & DllStructGetData($tInfo, "lParam") & @CRLF & _
                            "-->KeyFlags:" & @TAB & DllStructGetData($tInfo, "KeyFlags"))
                    ; No return value
                Case $NM_KILLFOCUS ; The control has lost the input focus
                    _DebugPrint("$NM_KILLFOCUS" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode)
                    ; No return value
                Case $NM_RCLICK ; Sent by a list-view control when the user clicks an item with the right mouse button
                    $tInfo = DllStructCreate($tagNMITEMACTIVATE, $lParam)
                    _DebugPrint("$NM_RCLICK" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode & @CRLF & _
                            "-->Index:" & @TAB & DllStructGetData($tInfo, "Index") & @CRLF & _
                            "-->SubItem:" & @TAB & DllStructGetData($tInfo, "SubItem") & @CRLF & _
                            "-->NewState:" & @TAB & DllStructGetData($tInfo, "NewState") & @CRLF & _
                            "-->OldState:" & @TAB & DllStructGetData($tInfo, "OldState") & @CRLF & _
                            "-->Changed:" & @TAB & DllStructGetData($tInfo, "Changed") & @CRLF & _
                            "-->ActionX:" & @TAB & DllStructGetData($tInfo, "ActionX") & @CRLF & _
                            "-->ActionY:" & @TAB & DllStructGetData($tInfo, "ActionY") & @CRLF & _
                            "-->lParam:" & @TAB & DllStructGetData($tInfo, "lParam") & @CRLF & _
                            "-->KeyFlags:" & @TAB & DllStructGetData($tInfo, "KeyFlags"))
                    ;Return 1 ; not to allow the default processing
                    Return 0 ; allow the default processing
                Case $NM_RDBLCLK ; Sent by a list-view control when the user double-clicks an item with the right mouse button
                    $tInfo = DllStructCreate($tagNMITEMACTIVATE, $lParam)
                    _DebugPrint("$NM_RDBLCLK" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode & @CRLF & _
                            "-->Index:" & @TAB & DllStructGetData($tInfo, "Index") & @CRLF & _
                            "-->SubItem:" & @TAB & DllStructGetData($tInfo, "SubItem") & @CRLF & _
                            "-->NewState:" & @TAB & DllStructGetData($tInfo, "NewState") & @CRLF & _
                            "-->OldState:" & @TAB & DllStructGetData($tInfo, "OldState") & @CRLF & _
                            "-->Changed:" & @TAB & DllStructGetData($tInfo, "Changed") & @CRLF & _
                            "-->ActionX:" & @TAB & DllStructGetData($tInfo, "ActionX") & @CRLF & _
                            "-->ActionY:" & @TAB & DllStructGetData($tInfo, "ActionY") & @CRLF & _
                            "-->lParam:" & @TAB & DllStructGetData($tInfo, "lParam") & @CRLF & _
                            "-->KeyFlags:" & @TAB & DllStructGetData($tInfo, "KeyFlags"))
                    ; No return value
                Case $NM_RETURN ; The control has the input focus and that the user has pressed the ENTER key
                    _DebugPrint("$NM_RETURN" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode)
                    ; No return value
                Case $NM_SETFOCUS ; The control has received the input focus
                    _DebugPrint("$NM_SETFOCUS" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode)
                    ; No return value
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>_WM_NOTIFY

Func _DebugPrint($s_Text, $sLine = @ScriptLineNumber)
;~  ConsoleWrite( _
;~          "!===========================================================" & @CRLF & _
;~          "+======================================================" & @CRLF & _
;~          "-->Line(" & StringFormat("%04d", $sLine) & "):" & @TAB & $s_Text & @CRLF & _
;~          "+======================================================" & @CRLF)
EndFunc   ;==>_DebugPrint

Func ListView_HOTTRACK($iSubItem)
    Local $iHotItem = _GUICtrlListView_GetHotItem($ListView1)
    If $iHotItem <> -1 Then
;~      ConsoleWrite("Hot Item: " & $iHotItem & " SubItem: " & $iSubItem & @CRLF)
    EndIf
EndFunc   ;==>ListView_HOTTRACK

 

Edited by DeltaRocked
Link to comment
Share on other sites

Here is only needed code, stripped unnecessary code + 

ConsoleWrite(">>>>>>>>>>> " & _GUICtrlListView_GetItemText($ListView1, $index, $SubItem) & @CRLF)

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

#include <MsgBoxConstants.au3>

Opt("GUIOnEventMode", 1)

Local $aItems[10][2]
For $iI = 0 To UBound($aItems) - 1
    $aItems[$iI][0] = "Row " & $iI
    $aItems[$iI][1] = "Item " & $iI
Next

$Form1 = GUICreate("Form1", 850, 830, -1, -1)
$ListView1 = GUICtrlCreateListView("Type|From ID", 13, 180, 826, 325, BitOR($LVS_SINGLESEL, $WS_HSCROLL, $WS_VSCROLL, $WS_BORDER))
_GUICtrlListView_SetExtendedListViewStyle($ListView1, BitOR($LVS_EX_ONECLICKACTIVATE, $LVS_EX_TRACKSELECT, $LVS_EX_FULLROWSELECT, $LVS_EX_GRIDLINES, $LVS_EX_HEADERDRAGDROP))
GUISetOnEvent($GUI_EVENT_CLOSE, "Form2Close")
_GUICtrlListView_AddArray($ListView1, $aItems)

GUISetState()
GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY")

GUISetState(@SW_SHOW)

While 1
    Sleep(100)
WEnd

Func Form2Close()
    Exit
EndFunc   ;==>Form2Close

Func _WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam
    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $event = DllStructGetData($tNMHDR, 'Code')

    If $wParam = $ListView1 And $event = $NM_CLICK Then
        $tInfo = DllStructCreate($tagNMITEMACTIVATE, $lParam)
        $index = DllStructGetData($tInfo, "Index")
        $SubItem = DllStructGetData($tInfo, "SubItem")
        ConsoleWrite(">>>>>>>>>>> " & _GUICtrlListView_GetItemText($ListView1, $index, $SubItem) & @CRLF)
    EndIf

    Return $GUI_RUNDEFMSG
EndFunc   ;==>_WM_NOTIFY

 

Link to comment
Share on other sites

@Zedna: that's a nice and clear demonstration.  Thanks.

A question, though (since I haven't worked with ListViews all that much):  Is there a way to inhibit the highlighting that occurs when the mouse hovers over a row?

 

(I'll add that just removing the selection params only works for the first column, but not for the second.)

$LVS_EX_TRACKSELECT, $LVS_EX_FULLROWSELECT
Edited by qwert
Link to comment
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
 Share

  • Recently Browsing   0 members

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