Jump to content

Problem selecting listview item to tick a checkbox


 Share

Recommended Posts

Good evening guys

Struggling to find away to fix this problem. I basicly want to click an item in the listview, and it ticks the checkbox for me on the side. at the moment im using WM_Notify to activate the checkbox, but the problem is now if i tick the checkbox the command is sent twice (once via WM_notify and once by me manualy clicking, nulling the operation). i'm not sure if NW_notify is the correct way of doing this, not sure if ive missed an ex-style or something which may accomplish this more easily. heres the code.

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

$search_gui = GUICreate("Product Scanner", 400, 300, -1, -1)
$listview = GUICtrlCreateListView("Product ID|Category|Last Update", 2, 2, 394, 250)
_GUICtrlListView_SetExtendedListViewStyle($listview, BitOR($LVS_EX_BORDERSELECT, $LVS_EX_FULLROWSELECT, $LVS_EX_CHECKBOXES, $LVS_EX_GRIDLINES))
GUISetState()

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

GUICtrlCreateListViewItem("1|2|3", $listview)
GUICtrlCreateListViewItem("4|5|6", $listview)
GUICtrlCreateListViewItem("7|8|9", $listview)

While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
WEnd
GUIDelete()

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView, $tInfo
    $hWndListView = $listview
    If Not IsHWnd($listview) Then $hWndListView = GUICtrlGetHandle($listview)

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    
    If $hWndFrom = $hWndListView And $iCode = $NM_CLICK Then
        $tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
        If DllStructGetData($tInfo, "Index") >= 0 Then
            If _GUICtrlListView_GetItemChecked($listview, DllStructGetData($tInfo, "Index")) = 0 Then
                _GUICtrlListView_SetItemChecked($listview, DllStructGetData($tInfo, "Index"), 1)
            Else
                _GUICtrlListView_SetItemChecked($listview, DllStructGetData($tInfo, "Index"), 0)
            EndIf
        EndIf
    EndIf
EndFunc

appreciate any help with this, thanks

Edited by laffo16
Link to comment
Share on other sites

you need to hittest and check that the click is not on a checkbox

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

Opt('MustDeclareVars', 1)

Local $msg
Local $search_gui = GUICreate("Product Scanner", 400, 300, -1, -1)
Local $listview = GUICtrlCreateListView("Product ID|Category|Last Update", 2, 2, 394, 250)
_GUICtrlListView_SetExtendedListViewStyle($listview, BitOR($LVS_EX_BORDERSELECT, $LVS_EX_FULLROWSELECT, $LVS_EX_CHECKBOXES, $LVS_EX_GRIDLINES))
GUISetState()

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

GUICtrlCreateListViewItem("1|2|3", $listview)
GUICtrlCreateListViewItem("4|5|6", $listview)
GUICtrlCreateListViewItem("7|8|9", $listview)

While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
WEnd
GUIDelete()

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView, $tInfo

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")

    If $iIDFrom = $listview Then
        Switch $iCode
            Case $NM_CLICK
                Local $tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
                If @error Then Return $GUI_RUNDEFMSG
                Local $Item = DllStructGetData($tInfo, "Index")
                If @error Or $Item = -1 Then Return $GUI_RUNDEFMSG

                Local $tTest = DllStructCreate($tagLVHITTESTINFO)
                DllStructSetData($tTest, "X", DllStructGetData($tInfo, "X"))
                DllStructSetData($tTest, "Y", DllStructGetData($tInfo, "Y"))
                Local $iRet = GUICtrlSendMsg($iIDFrom, $LVM_HITTEST, 0, DllStructGetPtr($tTest))
                If @error Or $iRet = -1 Then Return $GUI_RUNDEFMSG
                Switch DllStructGetData($tTest, "Flags")
                    Case $LVHT_ONITEMICON, $LVHT_ONITEMLABEL, $LVHT_ONITEM
                        If _GUICtrlListView_GetItemChecked($listview, $Item) = False Then
                            _GUICtrlListView_SetItemChecked($listview, $Item, 1)
                        Else
                            _GUICtrlListView_SetItemChecked($listview, $Item, 0)
                        EndIf
                    Case $LVHT_ONITEMSTATEICON ;on checkbox
                EndSwitch
        EndSwitch
    EndIf
    Return $GUI_RUNDEFMSG
EndFunc

I see fascists...

Link to comment
Share on other sites

  • 4 months later...

sorry to bump old thread, have just upgrades to win7 (64bit) from xp and rovers example above has been broken, anyone know how to fix this? any help appreciated.

DllStructGetData($tInfo, "Index") is returning 0 regardless of row being clicked. perhaps a problem with

$tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)?

Edited by laffo16
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...