gil900

Is this is a correct way to know if listview item was selected/un-selected ?

3 posts in this topic

#1 ·  Posted (edited)

 

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



Local $g_hListView

$hGUI = GUICreate("ListView Get Item Focused", 400, 300)
$g_hListView = GUICtrlCreateListView ("", 2, 2, 394, 268, BitOR($LVS_REPORT, $LVS_SHOWSELALWAYS), BitOR($WS_EX_CLIENTEDGE, $LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT))
;~ _GUICtrlListView_SetExtendedListViewStyle($g_hListView, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT))



; Add columns
_GUICtrlListView_AddColumn($g_hListView, "Column 1", 100)
_GUICtrlListView_AddColumn($g_hListView, "Column 2", 100)
_GUICtrlListView_AddColumn($g_hListView, "Column 3", 100)
_GUICtrlListView_AddColumn($g_hListView, "Column 4", 50)

; Add items
$it0 = GUICtrlCreateListViewItem('adwawd A',$g_hListView)
    _GUICtrlListView_AddSubItem($g_hListView, 0, "A.1", 1)
    _GUICtrlListView_AddSubItem($g_hListView, 0, "A.2", 2)
$it1 = GUICtrlCreateListViewItem('Bgsefgsf ',$g_hListView)
    _GUICtrlListView_AddSubItem($g_hListView, 1, "B.1", 1)

$it2 = GUICtrlCreateListViewItem('Cfe adwawd ',$g_hListView)
    _GUICtrlListView_AddSubItem($g_hListView, 2, "C.2", 2)

$it3 = GUICtrlCreateListViewItem('dadwadw ad',$g_hListView)
    _GUICtrlListView_AddSubItem($g_hListView, 3, "D.0", 0)


$it4 = GUICtrlCreateListViewItem('',$g_hListView)
    _GUICtrlListView_AddSubItem($g_hListView, 4, "E - Some text!!! dawdad", 0)
    _GUICtrlListView_AddSubItem($g_hListView, 4, "E some text 2 !!!", 1)
    _GUICtrlListView_AddSubItem($g_hListView, 4, "E some text 3 adwadwawd", 2)

$it5 = GUICtrlCreateListViewItem('',$g_hListView)
    _GUICtrlListView_AddSubItem($g_hListView, 5, "F - Some text!!! dawdad", 0)
    _GUICtrlListView_AddSubItem($g_hListView, 5, "F some text 2 !!!", 1)
    _GUICtrlListView_AddSubItem($g_hListView, 5, "F some text 3 adwadwawd", 2)



_GUICtrlListView_SetColumnWidth($g_hListView, 0, $LVSCW_AUTOSIZE_USEHEADER)
_GUICtrlListView_SetColumnWidth($g_hListView, 1, $LVSCW_AUTOSIZE_USEHEADER)
_GUICtrlListView_SetColumnWidth($g_hListView, 2, $LVSCW_AUTOSIZE_USEHEADER)
;~  _GUICtrlListView_SetColumnWidth($idListview, 3, $LVSCW_AUTOSIZE_USEHEADER)



GUISetState(@SW_SHOW)


GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")


Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE
GUIDelete()




Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
    Return ListView_WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
EndFunc







Func ListView_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 $LVN_ITEMCHANGED ; An item has changed
                     $tInfo = DllStructCreate($tagNMLISTVIEW, $lParam)

;~                      _DebugPrint("$LVN_ITEMCHANGED" & @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


                    $iNewState = DllStructGetData($tInfo, "NewState")
;~                  ;ConsoleWrite($iNewState &' (L: '&@ScriptLineNumber&')'&@CRLF)

                    $iTem = DllStructGetData($tInfo, "Item")
                    If BitAND($iNewState,$LVIS_SELECTED) Then
                        ToolTip('Item: '&$iTem&' selected') ; <----- Item X selected
                        ConsoleWrite( 'Item: '&$iTem&' selected'&' (L: '&@ScriptLineNumber&')'&@CRLF)
                    Else
                        ToolTip('Item: '&$iTem&' unselected') ; <----- Item X unselected
                        ConsoleWrite('Item: '&$iTem&' unselected' &' (L: '&@ScriptLineNumber&')'&@CRLF)
                    EndIf




            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

 

I try to do it with the "WM_NOTIFY" way. look at lines 115-119
it works but I am not sure that it is valid way to do this.

There is no good enough example in the help file to cover it. the only clue that I was able to get come from code sample from russian forum.
I don't see logic do it with _GUICtrlListView_GetItemState if I directly get the answer in WM_NOTIFY. Just need to know if I reed the  answer correctly.

Thank you.

 

EDIT:

I have bug in the code..
If I hold CTRL key and selecting for example the first item, it will write "Item 0 selected". this is fine and correct.
But if I continue to select for example the second item (while holding CTRL), then it write "Item 0 unselected" and "Item 1 selected".
"Item 0 unselected" is not correct because I holded the CTRL key and so I did not unselected it. I just selected for example the second Item in addition to the first one

 

 

EDIT:

No matter .. my code design was wrong. I  should only use the $LVN_ITEMCHANGED trigger and then from this trigger point call to function that handle it with _GUICtrlListView_GetItemState .. So it's good enough.  

Edited by gil900

Share this post


Link to post
Share on other sites



#2 ·  Posted

You must check in this way:

$iNewState = DllStructGetData($tInfo, "NewState")
$iOldState = DllStructGetData($tInfo, "OldState")
;ConsoleWrite($iNewState &' (L: '&@ScriptLineNumber&')'&@CRLF)

$iTem = DllStructGetData($tInfo, "Item")
If Not BitAND($iOldState,$LVIS_SELECTED) And BitAND($iNewState,$LVIS_SELECTED) Then
  ToolTip('Item: '&$iTem&' selected') ; <----- Item X selected
  ConsoleWrite( 'Item: '&$iTem&' selected'&' (L: '&@ScriptLineNumber&')'&@CRLF)
ElseIf BitAND($iOldState,$LVIS_SELECTED) And Not BitAND($iNewState,$LVIS_SELECTED) Then
  ToolTip('Item: '&$iTem&' unselected') ; <----- Item X unselected
  ConsoleWrite('Item: '&$iTem&' unselected' &' (L: '&@ScriptLineNumber&')'&@CRLF)
EndIf

 

1 person likes this

Share this post


Link to post
Share on other sites

#3 ·  Posted

15 minutes ago, LarsJ said:

You must check in this way:

$iNewState = DllStructGetData($tInfo, "NewState")
$iOldState = DllStructGetData($tInfo, "OldState")
;ConsoleWrite($iNewState &' (L: '&@ScriptLineNumber&')'&@CRLF)

$iTem = DllStructGetData($tInfo, "Item")
If Not BitAND($iOldState,$LVIS_SELECTED) And BitAND($iNewState,$LVIS_SELECTED) Then
  ToolTip('Item: '&$iTem&' selected') ; <----- Item X selected
  ConsoleWrite( 'Item: '&$iTem&' selected'&' (L: '&@ScriptLineNumber&')'&@CRLF)
ElseIf BitAND($iOldState,$LVIS_SELECTED) And Not BitAND($iNewState,$LVIS_SELECTED) Then
  ToolTip('Item: '&$iTem&' unselected') ; <----- Item X unselected
  ConsoleWrite('Item: '&$iTem&' unselected' &' (L: '&@ScriptLineNumber&')'&@CRLF)
EndIf

 

Thank you! Now it has passed all the basic of tests. After a second look I see that this is indeed a better way to do it

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