Jump to content

Recommended Posts

Posted

I'm drowning. Treeviews are my nemesis, alongside beetroot.

I have a Treeview with checked boxes.

How do I loop through each node, checking whether each node is checked?

<reaches for the Nurofen>

:)

Posted

I manged to cobble this together, but the code stops after the first item in the treeview... I need to it work through all items in the treeview

What am I doing wrong?

#include <GuiConstantsEx.au3>
#include <GuiTreeView.au3>
#include <WindowsConstants.au3>


_Main()

Func _Main()

    Local $hItem[10], $hTreeView
    Local $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_CHECKBOXES)
    
    GUICreate("TreeView Get Next", 400, 300)

    $hTreeView = GUICtrlCreateTreeView(2, 2, 396, 268, $iStyle, $WS_EX_CLIENTEDGE)
    GUISetState()

    _GUICtrlTreeView_BeginUpdate($hTreeView)
    For $x = 0 To 3
        $hItem[$x] = GUICtrlCreateTreeViewItem(StringFormat("[%02d] New Item", $x + 1), $hTreeView)
        For $y = 1 To Random(2, 10, 1)
            GUICtrlCreateTreeViewItem(StringFormat("[%02d] New Child", $y), $hItem[$x])
        Next
    Next
    $hItem[4] = GUICtrlCreateTreeViewItem(StringFormat("[%02d] New Item", 5), $hTreeView)
    For $x = 5 To 9
        $hItem[$x] = GUICtrlCreateTreeViewItem(StringFormat("[%02d] New Item", $x + 1), $hTreeView)
        For $y = 1 To Random(2, 10, 1)
            GUICtrlCreateTreeViewItem(StringFormat("[%02d] New Child", $y), $hItem[$x])
        Next
    Next
    _GUICtrlTreeView_EndUpdate($hTreeView)
    
    _GUICtrlTreeView_Expand($hTreeView)
    $h_itm = _GUICtrlTreeView_GetFirstItem($hTreeview)
    
    While 1
        $text = _GUICtrlTreeView_GetText($hTreeview, $h_itm)
        If _GUICtrlTreeView_GetChecked($hTreeview, $h_itm) = true Then
            msgbox(0, $text, "is checked")
        Else    
            msgbox(0, $text, "is NOT checked")
        EndIf
        
        $h_itm = _GUICtrlTreeView_GetNext($hTreeview, $h_itm)
        If $h_itm <= 0 then exitloop
        
    WEnd
    

    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
    GUIDelete()
EndFunc  ;==>_Main
Posted

Replace

If $h_itm <= 0 then exitloop

by

If $h_itm = 0 then exitloop

Also you mixed up control ID versus control handle.

control ID is used in AutoIt's native functions

and control handle is used in UDFs.

You are lucky because many UDFs are written in way it correctly handles also control ID's instead of handles.

But generally you shouldn't rely on this.

#include <GuiConstantsEx.au3>
#include <GuiTreeView.au3>
#include <WindowsConstants.au3>

_Main()

Func _Main()

    Local $hItem[10], $hTreeView
    Local $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_CHECKBOXES)
    
    GUICreate("TreeView Get Next", 400, 300)

    $idTreeView = GUICtrlCreateTreeView(2, 2, 396, 268, $iStyle, $WS_EX_CLIENTEDGE)
    $hTreeView = GUICtrlGetHandle($idTreeView)
    GUISetState()

    _GUICtrlTreeView_BeginUpdate($hTreeView)
    For $x = 0 To 3
        $hItem[$x] = GUICtrlCreateTreeViewItem(StringFormat("[%02d] New Item", $x + 1), $idTreeView)
        For $y = 1 To Random(2, 10, 1)
            GUICtrlCreateTreeViewItem(StringFormat("[%02d] New Child", $y), $hItem[$x])
        Next
    Next
    $hItem[4] = GUICtrlCreateTreeViewItem(StringFormat("[%02d] New Item", 5), $idTreeView)
    For $x = 5 To 9
        $hItem[$x] = GUICtrlCreateTreeViewItem(StringFormat("[%02d] New Item", $x + 1), $idTreeView)
        For $y = 1 To Random(2, 10, 1)
            GUICtrlCreateTreeViewItem(StringFormat("[%02d] New Child", $y), $hItem[$x])
        Next
    Next
    _GUICtrlTreeView_EndUpdate($hTreeView)
    
    _GUICtrlTreeView_Expand($hTreeView)
    $h_itm = _GUICtrlTreeView_GetFirstItem($hTreeview)
    
    While 1
        $text = _GUICtrlTreeView_GetText($hTreeview, $h_itm)
        If _GUICtrlTreeView_GetChecked($hTreeview, $h_itm) = true Then
;            msgbox(0, $text, "is checked")
            ConsoleWrite($text & " is checked" & @CRLF)
        Else    
;            msgbox(0, $text, "is NOT checked")
            ConsoleWrite($text & " is NOT checked" & @CRLF)
        EndIf
        
        $h_itm = _GUICtrlTreeView_GetNext($hTreeview, $h_itm)
        If $h_itm = 0 then exitloop
        
    WEnd

    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
    GUIDelete()
EndFunc  ;==>_Main

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
×
×
  • Create New...