TreeView Items deaktivieren und mehr

  • Hi,
    ich will mir eine GUI mit TreeView aufbauen.
    Wenn ich ein Item anhake soll sofort (ohne einen Button zu drücken) die Subitems abgehakt und deaktiviert werden.
    Wenn ich ein Item abhake soll sofort (ohne einen Button zu drücken) die Subitems aktiviert werden.

    Wie mache ich das.

    PS: Wie würdet ihr z. B. Dim $hauptitem[10] dimensionieren, da sich die Anzahl der Items variabel sein wird?

    Spoiler anzeigen
    [autoit]

    #include <GuiTreeView.au3>
    #include <GUIConstantsEx.au3>
    #include <WindowsConstants.au3>
    #include <TreeViewConstants.au3>

    [/autoit] [autoit][/autoit] [autoit]

    Dim $hauptitem[10]
    Dim $hItem[10][10]
    Dim $status[10][10]
    Dim $status_hauptitem[10]

    [/autoit] [autoit][/autoit] [autoit]

    Local $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_CHECKBOXES)
    $GUI = GUICreate("(UDF Created) TreeView Create", 400, 300)

    [/autoit] [autoit][/autoit] [autoit]

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

    [/autoit] [autoit][/autoit] [autoit]

    _GUICtrlTreeView_BeginUpdate($hTreeView)
    For $x = 1 To 4
    $hauptitem[$x] = _GUICtrlTreeView_Add($hTreeView, 0, StringFormat("[%02d] New Item", $x))
    For $y = 1 To 5
    $hItem[$x][$y] = _GUICtrlTreeView_AddChild($hTreeView, $hauptitem[$x], StringFormat("[%02d] New Child", $y))
    Next
    Next
    _GUICtrlTreeView_EndUpdate($hTreeView)

    [/autoit] [autoit][/autoit] [autoit]

    Sleep(3000)

    [/autoit] [autoit][/autoit] [autoit]

    For $x = 1 To 4
    $status_hauptitem[$x] = _GUICtrlTreeView_GetChecked($hTreeView, $hauptitem[$x])
    $text_hauptitem = _GUICtrlTreeView_GetText($hTreeView, $hauptitem[$x])
    If $status_hauptitem[$x] = True Then
    ConsoleWrite($text_hauptitem & @CRLF)
    Else
    For $y = 1 To 5
    $text_subitem = _GUICtrlTreeView_GetText($hTreeView, $hItem[$x][$y])
    $status[$x][$y] = _GUICtrlTreeView_GetChecked($hTreeView, $hItem[$x][$y])
    If $status[$x][$y] = True Then ConsoleWrite($text_hauptitem & " | " & $text_subitem & @CRLF)
    Next
    EndIf
    Next

    [/autoit] [autoit][/autoit] [autoit]

    ; Loop until user exits
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
    GUIDelete()
    ;~ EndFunc ;==>_Main

    [/autoit]
    • Offizieller Beitrag

    Ich handhabe das dynamisch, also definieren als $arTreeItem[1][2]. Bei Erstellen eines Item wird das Array mit ReDim vergrößert und die die ID in [n][0] eingetragen. In [n][1] hinterlege ich dann den Text des Item, das erleichert die Auswertung beim Zugriff.

  • Hi,
    als das mit dem dimensionieren hab ich so gemacht wie BugFix es meint, klappt gut.

    @pee
    könntest du mal in IOM4 nachsehen wie du es dort genau gemacht hast 8o

    • Offizieller Beitrag

    Das mit dem anhaken könnte man so lösen:

    Spoiler anzeigen
    [autoit]


    #include <GuiTreeView.au3>
    #include <GUIConstantsEx.au3>
    #include <WindowsConstants.au3>
    #include <TreeViewConstants.au3>

    [/autoit] [autoit][/autoit] [autoit]

    Global $hauptitem[10]
    Global $hItem[10][10]
    Global $status[10][10]
    Global $status_hauptitem[10]

    [/autoit] [autoit][/autoit] [autoit]

    Local $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_CHECKBOXES)
    $GUI = GUICreate("(UDF Created) TreeView Create", 400, 300)

    [/autoit] [autoit][/autoit] [autoit]

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

    [/autoit] [autoit][/autoit] [autoit]

    _GUICtrlTreeView_BeginUpdate($hTreeView)
    For $x = 1 To 4
    $hauptitem[$x] = _GUICtrlTreeView_Add($hTreeView, 0, StringFormat("[%02d] New Item", $x))
    For $y = 1 To 5
    $hItem[$x][$y] = _GUICtrlTreeView_AddChild($hTreeView, $hauptitem[$x], StringFormat("[%02d] New Child", $y))
    Next
    Next
    _GUICtrlTreeView_EndUpdate($hTreeView)

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Do
    For $x = 1 To 4
    $status_hauptitem[$x] = _GUICtrlTreeView_GetChecked($hTreeView, $hauptitem[$x])
    $text_hauptitem = _GUICtrlTreeView_GetText($hTreeView, $hauptitem[$x])
    If $status_hauptitem[$x] Then
    For $y = 1 To 5
    _GUICtrlTreeView_SetChecked($hTreeView, $hItem[$x][$y], True)
    Next
    ElseIf Not $status_hauptitem[$x] And _CheckItems($x) = 5 Then
    For $y = 1 To 5
    _GUICtrlTreeView_SetChecked($hTreeView, $hItem[$x][$y], False)
    Next
    EndIf
    Next
    Until GUIGetMsg() = $GUI_EVENT_CLOSE

    [/autoit] [autoit][/autoit] [autoit]

    GUIDelete()

    [/autoit] [autoit][/autoit] [autoit]

    Func _CheckItems($x)
    Local $check = 0
    For $y = 1 To 5
    If _GUICtrlTreeView_GetChecked($hTreeView, $hItem[$x][$y]) Then $check += 1
    Next
    Return $check
    EndFunc

    [/autoit]
  • Danke Oscar,
    damit hast du mir auf die Sprünge geholfen.
    Deins hat nur teilweise funktioniert :P

    So klappt es nun einwandfrei :thumbup:

    Spoiler anzeigen
    [autoit]

    #include <GuiTreeView.au3>
    #include <GUIConstantsEx.au3>
    #include <TreeViewConstants.au3>
    #include <WindowsConstants.au3>
    #include <Array.au3>

    [/autoit] [autoit][/autoit] [autoit]

    Global $hauptitem[20]
    Global $hItem[20][20]

    [/autoit] [autoit][/autoit] [autoit]

    Local $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_CHECKBOXES)
    $GUI = GUICreate("(UDF Created) TreeView Create", 400, 300)

    [/autoit] [autoit][/autoit] [autoit]

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

    [/autoit] [autoit][/autoit] [autoit]

    _GUICtrlTreeView_BeginUpdate($hTreeView)
    For $x = 1 To Random(3, 10, 1)
    $hauptitem[$x] = _GUICtrlTreeView_Add($hTreeView, 0, StringFormat("[%02d] New Item", $x))

    For $y = 1 To Random(3, 10, 1)
    $hItem[$x][$y] = _GUICtrlTreeView_AddChild($hTreeView, $hauptitem[$x], StringFormat("[%02d] New Child", $y))
    Next
    Next
    _GUICtrlTreeView_EndUpdate($hTreeView)

    [/autoit] [autoit][/autoit] [autoit]

    $anzahl_item = $x
    Dim $array_merken[$anzahl_item + 1]

    [/autoit] [autoit][/autoit] [autoit]

    Dim $status_hauptitem[UBound($hauptitem)]
    Dim $status[UBound($hItem, 1)][UBound($hItem, 2)]

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Do
    For $x = 1 To $anzahl_item
    $status_hauptitem[$x] = _GUICtrlTreeView_GetChecked($hTreeView, $hauptitem[$x])
    $text_hauptitem = _GUICtrlTreeView_GetText($hTreeView, $hauptitem[$x])
    $anzahl_subitem = _GUICtrlTreeView_GetChildCount($hTreeView, $hauptitem[$x])

    [/autoit] [autoit][/autoit] [autoit]

    If $status_hauptitem[$x] And $array_merken[$x] <> $status_hauptitem[$x] Then ;Hauptitem angehakt >>> alle Subitem anhaken
    For $y = 1 To $anzahl_subitem
    _GUICtrlTreeView_SetChecked($hTreeView, $hItem[$x][$y], True)
    Next
    $array_merken[$x] = $status_hauptitem[$x]
    ConsoleWrite(1 & " " & $array_merken[$x] & @CRLF)

    ElseIf Not $status_hauptitem[$x] And $array_merken[$x] <> $status_hauptitem[$x] And _CheckItems($x) = $anzahl_subitem Then ;Hauptitem abgehakt >>> alle Subitem abhaken
    For $y = 1 To $anzahl_subitem
    _GUICtrlTreeView_SetChecked($hTreeView, $hItem[$x][$y], False)
    Next
    $array_merken[$x] = $status_hauptitem[$x]
    ConsoleWrite(2 & " " & $array_merken[$x] & @CRLF)

    ElseIf Not $status_hauptitem[$x] And $array_merken[$x] = $status_hauptitem[$x] And _CheckItems($x) = $anzahl_subitem Then ;alle Subitem angehakt >>> Hauptitem anhaken
    _GUICtrlTreeView_SetChecked($hTreeView, $hauptitem[$x], True)
    $status_hauptitem[$x] = _GUICtrlTreeView_GetChecked($hTreeView, $hauptitem[$x])
    $array_merken[$x] = $status_hauptitem[$x]
    ConsoleWrite(3 & " " & $array_merken[$x] & @CRLF)

    ElseIf $status_hauptitem[$x] And $array_merken[$x] = $status_hauptitem[$x] And _CheckItems($x) <= $anzahl_subitem - 1 Then ;1 Subitem abgehakt >>> Hauptitem abhaken
    _GUICtrlTreeView_SetChecked($hTreeView, $hauptitem[$x], False)
    $status_hauptitem[$x] = _GUICtrlTreeView_GetChecked($hTreeView, $hauptitem[$x])
    $array_merken[$x] = $status_hauptitem[$x]
    ConsoleWrite(4 & " " & $array_merken[$x] & @CRLF)
    EndIf
    Next
    Sleep(5)
    Until GUIGetMsg() = $GUI_EVENT_CLOSE

    [/autoit] [autoit][/autoit] [autoit]

    GUIDelete()

    [/autoit] [autoit][/autoit] [autoit]

    Func _CheckItems($x)
    Local $check = 0
    For $y = 1 To $anzahl_subitem
    If _GUICtrlTreeView_GetChecked($hTreeView, $hItem[$x][$y]) Then $check += 1
    Next
    Return $check
    EndFunc ;==>_CheckItems

    [/autoit]
  • Hi,
    ich will das ganze nun trennen.

    Das Skript mit der GUI

    Spoiler anzeigen
    [autoit]

    #include <Array.au3>
    #include <GuiTreeView.au3>
    #include <eigen_treeview.au3>
    #include <TreeViewConstants.au3>
    #include <WindowsConstants.au3>

    [/autoit] [autoit][/autoit] [autoit]

    Dim $hauptitem[1]
    Dim $hItem[1][1]

    [/autoit] [autoit][/autoit] [autoit]

    Local $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_CHECKBOXES)
    $GUI = GUICreate("(UDF Created) TreeView Create", 1200, 300)

    [/autoit] [autoit][/autoit] [autoit]

    $treeView = _GUICtrlTreeView_Create($GUI, 2, 2, 1196, 268, $iStyle, $WS_EX_CLIENTEDGE)

    [/autoit] [autoit][/autoit] [autoit]

    _GUICtrlTreeView_BeginUpdate($treeView)
    For $x = 1 To Random(3, 10, 1)
    ReDim $hauptitem[UBound($hauptitem) + 1]
    $hauptitem[$x] = _GUICtrlTreeView_Add($treeView, 0, StringFormat("[%02d] New Item", $x))

    [/autoit] [autoit][/autoit] [autoit]

    ReDim $hItem[UBound($hItem, 1) + 1][20]
    ;~ _ArrayDisplay($hItem)
    For $y = 1 To Random(3, 10, 1)
    $hItem[$x][$y] = _GUICtrlTreeView_AddChild($treeView, $hauptitem[$x], StringFormat("[%02d] New Child", $y))
    Next
    Next
    _GUICtrlTreeView_EndUpdate($treeView)

    [/autoit] [autoit][/autoit] [autoit]

    GUISetState()

    [/autoit] [autoit][/autoit] [autoit]

    $anzahl_item = $x - 1

    [/autoit] [autoit][/autoit] [autoit]

    Dim $array_merken[$anzahl_item + 1]
    Dim $status_hauptitem[UBound($hauptitem) + 1]
    Dim $status[UBound($hItem, 1)][UBound($hItem, 2)]

    [/autoit] [autoit][/autoit] [autoit]

    While 1
    $msg = GUIGetMsg()
    Select
    ;------------------------------------------------------------ Buttons ------------------------------------------------------
    Case $msg = $GUI_EVENT_CLOSE
    Exit
    EndSelect
    _treeview_an_abhaken($treeView, $hauptitem, $status_hauptitem, $hItem, $array_merken, $anzahl_item)
    WEnd

    [/autoit]


    Die UDF:

    Spoiler anzeigen
    [autoit]

    #include-once
    #include <GuiTreeView.au3>
    #include <GUIConstantsEx.au3>

    [/autoit] [autoit][/autoit] [autoit]

    ;*****************************************************************************************************
    ; Funktion _treeview_an_abhaken()
    ;
    ; Beschreibung automatisches anhaken der Hauptitem bzw. Subitem
    ;
    ; Parameter ...
    ;
    ; Return ---
    ;
    ; Bemerkung ...
    ; ...
    ;*****************************************************************************************************

    [/autoit] [autoit][/autoit] [autoit]

    Func _treeview_an_abhaken($udf_treeView, $hauptitem, $status_hauptitem, $hItem, $array_merken, $anzahl_item)
    Do
    For $x = 1 To $anzahl_item
    $status_hauptitem[$x] = _GUICtrlTreeView_GetChecked($udf_treeView, $hauptitem[$x])
    $text_hauptitem = _GUICtrlTreeView_GetText($udf_treeView, $hauptitem[$x])
    $anzahl_subitem = _GUICtrlTreeView_GetChildCount($udf_treeView, $hauptitem[$x])

    [/autoit] [autoit][/autoit] [autoit]

    ;prüfen wie viele Items angekreuzt sind
    $check = 0
    For $y = 1 To $anzahl_subitem
    If _GUICtrlTreeView_GetChecked($udf_treeView, $hItem[$x][$y]) Then $check += 1
    Next

    [/autoit] [autoit][/autoit] [autoit]

    If $status_hauptitem[$x] And $array_merken[$x] <> $status_hauptitem[$x] Then ;Hauptitem angehakt >>> alle Subitem anhaken
    For $y = 1 To $anzahl_subitem
    _GUICtrlTreeView_SetChecked($udf_treeView, $hItem[$x][$y], True)
    Next
    $array_merken[$x] = $status_hauptitem[$x]
    ConsoleWrite(1 & " " & $array_merken[$x] & @CRLF)

    [/autoit] [autoit][/autoit] [autoit]

    ElseIf Not $status_hauptitem[$x] And $array_merken[$x] <> $status_hauptitem[$x] And $check = $anzahl_subitem Then ;Hauptitem abgehakt >>> alle Subitem abhaken
    For $y = 1 To $anzahl_subitem
    _GUICtrlTreeView_SetChecked($udf_treeView, $hItem[$x][$y], False)
    Next
    $array_merken[$x] = $status_hauptitem[$x]
    ConsoleWrite(2 & " " & $array_merken[$x] & @CRLF)

    [/autoit] [autoit][/autoit] [autoit]

    ElseIf Not $status_hauptitem[$x] And $array_merken[$x] = $status_hauptitem[$x] And $check = $anzahl_subitem Then ;alle Subitem angehakt >>> Hauptitem anhaken
    _GUICtrlTreeView_SetChecked($udf_treeView, $hauptitem[$x], True)
    $status_hauptitem[$x] = _GUICtrlTreeView_GetChecked($udf_treeView, $hauptitem[$x])
    $array_merken[$x] = $status_hauptitem[$x]
    ConsoleWrite(3 & " " & $array_merken[$x] & @CRLF)

    [/autoit] [autoit][/autoit] [autoit]

    ElseIf $status_hauptitem[$x] And $array_merken[$x] = $status_hauptitem[$x] And $check <= $anzahl_subitem - 1 Then ;1 Subitem abgehakt >>> Hauptitem abhaken
    _GUICtrlTreeView_SetChecked($udf_treeView, $hauptitem[$x], False)
    $status_hauptitem[$x] = _GUICtrlTreeView_GetChecked($udf_treeView, $hauptitem[$x])
    $array_merken[$x] = $status_hauptitem[$x]
    ConsoleWrite(4 & " " & $array_merken[$x] & @CRLF)
    EndIf
    Next
    Sleep(5)
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
    EndFunc ;==>_treeview_an_abhaken

    [/autoit]

    Jedoch habe ich nun das Problem, dass ich nicht mehr aus der Schleife der UDF rauskomme.
    Habt ihr ne Idee wie man dies lösen könnte.

    Könnte man sonst noch was verbessern, vereinfachen ?(

    • Offizieller Beitrag

    Ich habe mich heute etwas ausführlicher mit den TreeView-Befehlen befasst und dabei ist mir noch eine andere Möglichkeit zum de-/markieren der Checkboxen eingefallen:

    Spoiler anzeigen
    [autoit]


    #include<GUIConstantsEx.au3>
    #include<GuiTreeView.au3>
    #include<TreeViewConstants.au3>
    #include<WindowsConstants.au3>

    [/autoit] [autoit][/autoit] [autoit]

    Dim $hauptitem[10]
    Dim $hItem[10][10]
    Dim $status[10][10]
    Dim $status_hauptitem[10]

    [/autoit] [autoit][/autoit] [autoit]

    Local $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_CHECKBOXES)
    $hGui = GUICreate("(UDF Created) TreeView Create", 400, 450)

    [/autoit] [autoit][/autoit] [autoit]

    $hTreeView = GUICtrlCreateTreeView(2, 2, 396, 446, $iStyle, $WS_EX_CLIENTEDGE)
    $hRoot = GUICtrlCreateTreeViewItem("Root", $hTreeView)

    [/autoit] [autoit][/autoit] [autoit]

    _GUICtrlTreeView_BeginUpdate($hTreeView)
    For $x = 1 To 4
    $hauptitem[$x] = _GUICtrlTreeView_AddChild($hTreeView, GUICtrlGetHandle($hRoot), StringFormat("[%02d] New Item", $x))
    For $y = 1 To 5
    $hItem[$x][$y] = _GUICtrlTreeView_AddChild($hTreeView, $hauptitem[$x], StringFormat("[%02d] New Child", $y))
    Next
    Next
    _GUICtrlTreeView_EndUpdate($hTreeView)
    _GUICtrlTreeView_Expand($hTreeView)
    GUISetState()

    [/autoit] [autoit][/autoit] [autoit]

    #cs
    $iAdjust = Korrekturwert, weil die GUIGetCursorInfo-Werte relativ zur GUI sind,
    _GUICtrlTreeView_HitTest() aber Werte relativ zum TreeView benötigt.
    #ce
    $iAdjust = 4
    While True
    Switch GUIGetMsg()
    Case $GUI_EVENT_PRIMARYDOWN
    $aInfo = GUIGetCursorInfo($hGui)
    If $aInfo[4] = $hTreeView Then
    If BitAND(_GUICtrlTreeView_HitTest($hTreeView, $aInfo[0]-$iAdjust, $aInfo[1]), 64) Then
    $checked = _GUICtrlTreeView_GetChecked($hTreeView, _GUICtrlTreeView_GetSelection($hTreeView))
    $hItem = _GUICtrlTreeView_GetFirstChild($hTreeView, _GUICtrlTreeView_GetSelection($hTreeView))
    If $hItem Then _MarkChildItems($hTreeView, $hItem, $checked)
    EndIf
    EndIf
    Case $GUI_EVENT_CLOSE
    Exit
    EndSwitch
    WEnd

    [/autoit] [autoit][/autoit] [autoit]

    Func _MarkChildItems($hTreeView, $hItem, $checked)
    Do
    $hNewItem = _GUICtrlTreeView_GetFirstChild($hTreeView, $hItem)
    If $hNewItem Then _MarkChildItems($hTreeView, $hNewItem, $checked)
    _GUICtrlTreeView_SetChecked($hTreeView, $hItem, $checked)
    $hItem = _GUICtrlTreeView_GetNextChild($hTreeView, $hItem)
    Until $hItem = 0
    EndFunc

    [/autoit]