Jump to content

Select Checkboxes and display


DigDeep
 Share

Recommended Posts

I have prepared the below code. Can someone help me to get this fixed to display all the CheckBoxes Checked when SUBMIT button is pressed.

If GHI is checked inside Option 1 > False > DEF > GHI and pressed Submit button then Msgbox would display as:

Option 1
False
DEF
GHI

Can someone please help here.

 

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

#Region ### START Koda GUI section ### Form=

; Create flag to indicate item selection change
Global $ItemSelected = False
; Create array to hold item data
Global $TreeMenu[17][3]

; Create GUI
Global $CT = GUICreate("CT", 1896, 984, 3, 2, BitOR($GUI_SS_DEFAULT_GUI, $WS_MAXIMIZEBOX, $WS_SIZEBOX, $WS_THICKFRAME, $WS_TABSTOP))

; Create TreeView
Global $Check = GUICtrlCreateTreeView(10, 10, 600, 900, BitOR($GUI_SS_DEFAULT_TREEVIEW, $TVS_CHECKBOXES, $TVS_HASLINES, $TVS_NONEVENHEIGHT))

;$Label1 = GUICtrlCreateLabel("SCCM Scenarios", 472, 16, 45, 20)
; Fill TreeView
$TreeMenu[0][2]  = GUICtrlCreateTreeViewItem("  Option 1", $Check)
$TreeMenu[1][2]  = GUICtrlCreateTreeViewItem("  True", $TreeMenu[0][2])
$TreeMenu[2][2]  = GUICtrlCreateTreeViewItem("  ABC", $TreeMenu[1][2])
$TreeMenu[3][2]  = GUICtrlCreateTreeViewItem("  False", $TreeMenu[0][2])
$TreeMenu[4][2]  = GUICtrlCreateTreeViewItem("  DEF", $TreeMenu[3][2])
$TreeMenu[5][2]  = GUICtrlCreateTreeViewItem("  GHI", $TreeMenu[4][2])
$TreeMenu[6][2]  = GUICtrlCreateTreeViewItem("  MayBe", $TreeMenu[0][2])
$TreeMenu[7][2]  = GUICtrlCreateTreeViewItem("  JKL", $TreeMenu[6][2])

$TreeMenu[8][2]  = GUICtrlCreateTreeViewItem("  Option 2", $Check)
$TreeMenu[9][2]  = GUICtrlCreateTreeViewItem("  True", $TreeMenu[8][2])
$TreeMenu[10][2]  = GUICtrlCreateTreeViewItem("  MNO", $TreeMenu[9][2])
$TreeMenu[11][2]  = GUICtrlCreateTreeViewItem("  False", $TreeMenu[8][2])
$TreeMenu[12][2]  = GUICtrlCreateTreeViewItem("  PQR", $TreeMenu[11][2])
$TreeMenu[13][2]  = GUICtrlCreateTreeViewItem("  STU", $TreeMenu[12][2])


; Set initial unchecked state and get item handles (needed for later code)
For $i = 0 To UBound($TreeMenu) - 1
    $TreeMenu[$i][1] = False
    $TreeMenu[$i][0] = GUICtrlGetHandle($TreeMenu[$i][2])
Next

; Expand TreeView
;_GUICtrlTreeView_Expand($Check)

; Create buttons to set/clear all items
Global $Select_All = GUICtrlCreateButton("Select All", 20, 930, 144, 30)
Global $Clear_All = GUICtrlCreateButton("Clear All", 188, 930, 152, 30)
Global $Copy = GUICtrlCreateButton("Submit", 448, 928, 147, 33)

GUISetState()

; Register message to look for TV item selection change
GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY")
#EndRegion ### END Koda GUI section ###

Global $bState, $iItemIndex, $sChkValues
While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Select_All
            _Adjust_All()
        Case $Clear_All
            _Adjust_All(False)

        Case $Copy
        ; Copy to Clipboard


    EndSwitch

    ; An item has been selected
    If $ItemSelected Then
        ; Determine checked state
        $bState = _GUICtrlTreeView_GetChecked($Check, $ItemSelected)
        ; Find item in array
        $iItemIndex = _ArraySearch($TreeMenu, $ItemSelected)
        ; If checked state has altered
        If $TreeMenu[$iItemIndex][1] <> $bState Then
            ; Store new state
            $TreeMenu[$iItemIndex][1] = $bState
            ; Adjust parents and children as required
            _Adjust_Parents($ItemSelected, $bState)
            _Adjust_Children($ItemSelected, $bState)
        EndIf
        ; Clear flag
        $ItemSelected = 0
    EndIf

WEnd

Func _Adjust_All($bState = True)

    ; Loop through items
    For $i = 0 To UBound($TreeMenu) - 1
        ; Adjust item
        _GUICtrlTreeView_SetChecked($Check, $TreeMenu[$i][0], $bState)
        ; Adjust array
        $TreeMenu[$i][1] = $bState
    Next

EndFunc   ;==>_Adjust_All

Func _Adjust_Parents($hPassedItem, $bState = True)

    Local $iIndex

    ; Get the handle of the parent
    Local $hParent = _GUICtrlTreeView_GetParentHandle($Check, $hPassedItem)
    If $hParent = 0 Then Return
    ; Assume parent is to be adjusted
    Local $bAdjustParent = True
    ; Need to confirm all siblings clear before clearing parent
    If $bState = False Then
        ; Check on number of siblings
        Local $iCount = _GUICtrlTreeView_GetChildCount($Check, $hParent)
        ; If only 1 sibling then parent can be cleared - if more then need to look at them all
        If $iCount <> 1 Then
            ; Number of siblings checked
            Local $iCheckCount = 0
            ; Move through previous siblings
            Local $hSibling = $hPassedItem
            While 1
                $hSibling = _GUICtrlTreeView_GetPrevSibling($Check, $hSibling)
                ; If found
                If $hSibling Then
                    ; Is sibling checked)
                    If _GUICtrlTreeView_GetChecked($Check, $hSibling) Then
                        ; Increase count if so
                        $iCheckCount += 1
                    EndIf
                Else
                    ; No point in continuing
                    ExitLoop
                EndIf
            WEnd
            ; Move through later siblings
            $hSibling = $hPassedItem
            While 1
                $hSibling = _GUICtrlTreeView_GetNextSibling($Check, $hSibling)
                If $hSibling Then
                    If _GUICtrlTreeView_GetChecked($Check, $hSibling) Then
                        $iCheckCount += 1
                    EndIf
                Else
                    ExitLoop
                EndIf
            WEnd
            ; If at least one sibling checked then do not clear parent
            If $iCheckCount Then $bAdjustParent = False
        EndIf
    EndIf
    ; If parent is to be adjusted
    If $bAdjustParent Then
        ; Adjust the array
        $iIndex = _ArraySearch($TreeMenu, $hParent)
        If @error Then Return
        $TreeMenu[$iIndex][1] = $bState
        ; Adjust the parent
        _GUICtrlTreeView_SetChecked($Check, $hParent, $bState)
        ; And now do the same for the generation above
        _Adjust_Parents($hParent, $bState)
    EndIf

EndFunc   ;==>_Check_Parents

Func _Adjust_Children($hPassedItem, $bState = True)

    Local $iIndex

    ; Get the handle of the first child
    Local $hChild = _GUICtrlTreeView_GetFirstChild($Check, $hPassedItem)
    If $hChild = 0 Then Return
    ; Loop through children
    While 1
        ; Adjust the array
        $iIndex = _ArraySearch($TreeMenu, $hChild)
        If @error Then Return
        $TreeMenu[$iIndex][1] = $bState
        ; Adjust the child
        _GUICtrlTreeView_SetChecked($Check, $hChild, $bState)
        ; And now do the same for the generation beow
        _Adjust_Children($hChild, $bState)
        ; Now get next child
        $hChild = _GUICtrlTreeView_GetNextChild($Check, $hChild)
        ; Exit the loop if no more found
        If $hChild = 0 Then ExitLoop
    WEnd

EndFunc   ;==>_Adjust_Children

Func _WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)

    #forceref $hWnd, $iMsg, $wParam
    ; Create NMTREEVIEW structure
    Local $tStruct = DllStructCreate("struct;hwnd hWndFrom;uint_ptr IDFrom;INT Code;endstruct;" & _
            "uint Action;struct;uint OldMask;handle OldhItem;uint OldState;uint OldStateMask;" & _
            "ptr OldText;int OldTextMax;int OldImage;int OldSelectedImage;int OldChildren;lparam OldParam;endstruct;" & _
            "struct;uint NewMask;handle NewhItem;uint NewState;uint NewStateMask;" & _
            "ptr NewText;int NewTextMax;int NewImage;int NewSelectedImage;int NewChildren;lparam NewParam;endstruct;" & _
            "struct;long PointX;long PointY;endstruct", $lParam)
    Local $hWndFrom = DllStructGetData($tStruct, "hWndFrom")
    If $hWndFrom = GUICtrlGetHandle($Check) Then
        Switch  DllStructGetData($tStruct, "Code")
            Case $TVN_SELCHANGEDA, $TVN_SELCHANGEDW
                Local $hItem = DllStructGetData($tStruct, "NewhItem")
                ; Set flag to selected item handle
                If $hItem Then $ItemSelected = $hItem
        EndSwitch
    EndIf
EndFunc

 

Link to comment
Share on other sites

You can implement your copy code in this way:

Case $Copy
  For $i = 0 To UBound( $TreeMenu ) - 1
    If _GUICtrlTreeView_GetChecked( $Check, $TreeMenu[$i][0] ) And Not _GUICtrlTreeView_GetChildren( $Check, $TreeMenu[$i][0] ) Then
      Local $item = $TreeMenu[$i][0], $s = ""
      While $item
        $s = _GUICtrlTreeView_GetText( $Check, $item ) & $s
        $item = _GUICtrlTreeView_GetParentHandle( $Check, $item )
      WEnd
      MsgBox( 0, "", $s )
    EndIf
  Next

 

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...