benners

Determining a treeview's checkbox state when clicked

2 posts in this topic

I am trying to get the checked state of a treeview item when the user checks or unchecks the items checkbox. I want to be able to update a label with the number of checked items whenever one changes. I am using the code below but sometimes the wrong state is returned. Is there a better way?.

#include <GUIConstantsEx.au3>
#include <GuiImageList.au3>
#include <GuiTreeView.au3>
#include <MsgBoxConstants.au3>
#include <WindowsConstants.au3>

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

Global $idTreeView

Example()

Func Example()
    Local $aidItem[6], $hRandomItem
    Local $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_CHECKBOXES)

    GUICreate("TreeView Get Checked", 400, 300)

    $idTreeView = GUICtrlCreateTreeView(2, 2, 396, 268, $iStyle, $WS_EX_CLIENTEDGE)
    GUISetState(@SW_SHOW)

    _GUICtrlTreeView_BeginUpdate($idTreeView)
    For $x = 0 To UBound($aidItem) - 1
        $aidItem[$x] = GUICtrlCreateTreeViewItem(StringFormat("[%02d] New Item", $x + 1), $idTreeView)
    Next
    _GUICtrlTreeView_EndUpdate($idTreeView)

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

Func WM_NOTIFY($hWnd, $msg, $wParam, $lParam)
    #forceref $hWnd, $msg, $wParam
    Local $tNMHDR, $IdFrom, $iCode

    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $IdFrom = DllStructGetData($tNMHDR, "IdFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")

    Switch $IdFrom
        Case $idTreeView ; Multiple section listview.
            Switch $iCode
                Case $NM_CLICK, $NM_DBLCLK ; If double clicked.
                        consolewrite((_GUICtrlTreeView_GetChecked($idTreeView, _GUICtrlTreeView_GetSelection($idTreeView)) = 0) & @crlf)
            EndSwitch
    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

 

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Well I realised it wouldn't work anyway, one of the problems is trying to get the index of the item by using _GUICtrlTreeView_GetSelection, it also runs if you click on an empty space in the treeview.

I have some code I used for the same thing with a listview, this gets the area of the small icon and works well but I can't figure a way to do the same for a treeview

Func WM_NOTIFY($hWnd, $msg, $wParam, $lParam)
    Local $tNMHDR, $IdFrom, $iCode

    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $IdFrom = DllStructGetData($tNMHDR, "IdFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")

    Switch $IdFrom
        Case $HF_lv
            Switch $iCode
                Case $NM_CLICK, $NM_DBLCLK

                    Local $tInfo = DllStructCreate($tagNMITEMACTIVATE, $lParam)
                    Local $iIndex = DllStructGetData($tInfo, "Index")

                    If $iIndex <> -1 Then
                        Local $iX = DllStructGetData($tInfo, "X")
                        Local $aRect = _GUICtrlListView_GetItemRect($HF_lv, $iIndex, 2)
                        If ($iX < $aRect[0]) And ($iX >= 5) Then GetItem_Checked('Hotfix', _GUICtrlListView_GetItemChecked($HF_lv, $iIndex) = 0)
                    Else
                        Switch $iCode
                            Case $NM_DBLCLK
                        EndSwitch
                    EndIf
            EndSwitch
    EndSwitch

    Return $GUI_RUNDEFMSG

again it requires the use of _GUICtrlTreeView_GetSelection for the index.

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

    Local $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    Local $IdFrom = DllStructGetData($tNMHDR, "IdFrom")
    Local $iCode = DllStructGetData($tNMHDR, "Code")

    Switch $IdFrom
        Case $g_id_DeleteItem_tv
            Switch $iCode
                Case $NM_CLICK, $NM_DBLCLK ; If double clicked.
                        Local $aRect = _GUICtrlTreeView_DisplayRect($g_id_DeleteItem_tv, _GUICtrlTreeView_GetSelection($g_id_DeleteItem_tv))
                        ConsoleWrite($arect[0] & @crlf & $arect[1] & @crlf & $arect[2] & @crlf & $arect[3] & @crlf)
            EndSwitch
    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

 

Edited by benners

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