Jump to content

Recommended Posts

Posted (edited)

Hey guys

I have a treeview with 4 check boxes and a parent "All Items" Checkbox.

How can I check all checkboxes if the "All Items" option is used..

See attached pic for a better understanding

I have this function thus far....

$hItem = _GUICtrlTreeView_GetFirstItem($hTreeView)
    $iItemCount = _GUICtrlTreeView_GetCount($hTreeView) 
    
        For $n = 0 To $iItemCount - 1
            
            $sText = _GUICtrlTreeView_GetText($hTreeView, $hItem)
            $boolChecked = _GUICtrlTreeView_GetChecked($hTreeView, $hItem)
            ConsoleWrite("Debug:  Index: " & $n & "  Handle: " & $hItem & "  Text: " & $sText & "  Checked: " & $boolChecked & @LF)         
            $hItem = _GUICtrlTreeView_GetNext($hTreeView, $hItem)       
    
        EndIf
            
        Next

Any help would be greatly appreciated!

Cheers

EDIT: typo

post-2827-1203477404_thumb.jpg

Edited by lyledg
Posted (edited)

I have found this post relating to my post...

http://www.autoitscript.com/forum/index.php?showtopic=64545

However, it seems to use

Opt("GUIOnEventMode", 1)

Which breaks my gui especially if I have child windows...If I use this option they don't apprea

I know I am doing something wrong..

If someone has the time, could they please post an example of where Opt("GUIOnEventMode", 1) is used with child windows so that I can get a better understanding of it's use..I never have grasped that concept and would appreciate any help

Cheers

PS: MsCreator...great exmaple as always mate...you are a genius!

Edited by lyledg
Posted

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Posted

Here is an example without the OnEvent mode (and little improvements)...

#include <GUIConstants.au3>

Global $iTotal_Items = 10
Global $TVItemsID_Array[$iTotal_Items + 1]

GUICreate("TreeView Check All", 460, 300)

$TreeView = GUICtrlCreateTreeView(20, 20, 420, 260, _
    BitOR($GUI_SS_DEFAULT_TREEVIEW, $TVS_CHECKBOXES), $WS_EX_DLGMODALFRAME + $WS_EX_CLIENTEDGE)

$SelAll_TVItem = GUICtrlCreateTreeViewItem("Select all", $TreeView)
GUICtrlSetColor($SelAll_TVItem, 0xC0C0C0)

For $i = 1 To $iTotal_Items
    $TVItemsID_Array[$i] = GUICtrlCreateTreeViewItem("Item " & $i, $SelAll_TVItem)
Next

GUICtrlSetState($SelAll_TVItem, $GUI_EXPAND)

GUISetState()

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $SelAll_TVItem
            Local $State = $GUI_UNCHECKED
            
            If BitAND(GUICtrlRead($SelAll_TVItem), $GUI_CHECKED) Then
                $State = $GUI_CHECKED
                GUICtrlSetColor($SelAll_TVItem, 0x0)
                GUICtrlSetState($SelAll_TVItem, $GUI_DEFBUTTON + $GUI_CHECKED)
            Else
                GUICtrlSetColor($SelAll_TVItem, 0xC0C0C0)
                GUICtrlSetState($SelAll_TVItem, $GUI_UNCHECKED)
            EndIf

            For $a = 1 To UBound($TVItemsID_Array) - 1
                GUICtrlSetState($TVItemsID_Array[$a], $State)
            Next
        Case $TVItemsID_Array[1] To $TVItemsID_Array[$iTotal_Items]
            
            
            If AllIsUnChecked() Then
                GUICtrlSetColor($SelAll_TVItem, 0xC0C0C0)
                GUICtrlSetState($SelAll_TVItem, $GUI_UNCHECKED)
            ElseIf AllIsChecked() Then
                GUICtrlSetColor($SelAll_TVItem, 0x0)
                GUICtrlSetState($SelAll_TVItem, $GUI_DEFBUTTON + $GUI_CHECKED)
            Else
                GUICtrlSetColor($SelAll_TVItem, 0x696969)
                GUICtrlSetState($SelAll_TVItem, $GUI_CHECKED)
            EndIf
    EndSwitch
WEnd

Func AllIsUnChecked()
    For $i = 1 To UBound($TVItemsID_Array) - 1
        If BitAND(GUICtrlRead($TVItemsID_Array[$i]), $GUI_CHECKED) Then Return False
    Next

    Return True
EndFunc

Func AllIsChecked()
    For $i = 1 To UBound($TVItemsID_Array) - 1
        If BitAND(GUICtrlRead($TVItemsID_Array[$i]), $GUI_UNCHECKED) Then Return False
    Next

    Return True
EndFunc

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Posted (edited)

As always MS...you deliver!

But one last question, how could this be adapted for use with the new function _GUICtrlTreeView_Create, rather than GUICtrlCreateTreeView

Cheers, you really are a good programmer!

:)

Edit:

Replaced GUICtrlCreateTreeViewItem with GUICtrlCreateTreeView

Edited by lyledg
Posted (edited)

Here's generic approach, which works regardless of how the treeview or its items are created, and doesn't require to handle each control ID separately:

#include <GuiTreeView.au3>
#Include <WinAPI.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>

Global $hTreeView

$hGui = GUICreate("TreeView Check All", 460, 300)

$TreeView = GUICtrlCreateTreeView(20, 20, 420, 260, BitOR($GUI_SS_DEFAULT_TREEVIEW, $TVS_CHECKBOXES), BitOR($WS_EX_DLGMODALFRAME,$WS_EX_CLIENTEDGE))
$hTreeView = GUICtrlGetHandle($TreeView)
BuildExampleTree($TreeView) ;just a test

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Func WM_NOTIFY($hWnd, $Msg, $wParam, $lParam)
    Local $hWndFrom, $idFrom, $iCode, $tNMHDR = DllStructCreate("hwnd hwndFrom;int idFrom;int code", $lParam)
    $hWndFrom = DllStructGetData($tNMHDR, "hwndFrom")
    $iCode = DllStructGetData($tNMHDR, "code")
    If $hWndFrom = $hTreeView Then
        Switch $iCode
            Case $NM_CLICK
                ;;$tagTVHITTESTINFO = "int X;int Y;int Flags;int Item"
                Local $tMPos = _WinAPI_GetMousePos(True, $hWndFrom), $tHit = _GUICtrlTreeView_HitTestEx($hWndFrom, DllStructGetData($tMPos, 1), DllStructGetData($tMPos, 2))
                Local $hItem = DllStructGetData($tHit, "Item"), $iFlags = DllStructGetData($tHit, "Flags")
                If $hItem <> 0 And BitAND($iFlags, $TVHT_ONITEMSTATEICON) Then 
                    _TV_Checkbox_MultiSet($hWndFrom, $hItem, 1)
                EndIf
            Case $TVN_KEYDOWN
                Local $tNMTVKEY = DllStructCreate("hwnd;int;int;short key;uint", $lParam)
                Local $hSelected = _GUICtrlTreeView_GetSelection($hWndFrom)
                If DllStructGetData($tNMTVKEY, "key") = 0x20 And $hSelected Then ;;space
                    _TV_Checkbox_MultiSet($hWndFrom, $hSelected, 1)
                EndIf
            Case Else 
        EndSwitch
    EndIf
    Return $GUI_RUNDEFMSG
EndFunc
Func _TV_Checkbox_MultiSet($hWnd, $hItem, $fTop = 0)
    Local $fChecked = _GUICtrlTreeView_GetChecked($hWnd, $hItem)
    If $fTop Then $fChecked = Not $fChecked
    If _GUICtrlTreeView_GetChildren($hWnd, $hItem) Then
        Local $hChild = _GUICtrlTreeView_GetFirstChild($hWnd, $hItem)
        Do
            _GUICtrlTreeView_SetChecked($hWnd, $hChild, $fChecked)
            If _GUICtrlTreeView_GetChildren($hWnd, $hChild) Then
                _TV_Checkbox_MultiSet($hWnd, $hChild)
            EndIf
            $hChild = _GUICtrlTreeView_GetNextChild($hWnd, $hChild)
        Until $hChild = 0
    EndIf
EndFunc

Func BuildExampleTree($treeID)
    Local $ti, $tsi, $i, $j, $k
    $ti = GUICtrlCreateTreeViewItem("1", $treeID)
    For $i = 1 To 5
        $tsi = GUICtrlCreateTreeViewItem("1-" & $i, $ti)
        If $i = 3 Then
            For $j = 1 To 3
                $tssi = GUICtrlCreateTreeViewItem("1-" & $i & '-' & $j, $tsi)
                If $j = 2 Then
                    For $k = 1 To 4
                        GUICtrlCreateTreeViewItem("1-" & $i & '-' & $j & '-' & $k, $tssi)
                    Next
                EndIf
            Next
        EndIf
    Next
    GUICtrlCreateTreeViewItem("2", $treeID)
    ControlTreeView('', '', $treeID, "Expand", '#0')
    ControlTreeView('', '', $treeID, "Expand", '#0|#2')
    ControlTreeView('', '', $treeID, "Expand", '#0|#2|#1')
EndFunc
Edited by Siao

"be smart, drink your wine"

Posted

Great stuff Siao!!!

However, I see in the Func BuildExampleTree($treeID) you uses GUICtrlCreateTreeViewItem. I am wanting to use the newer _GUICtrlTreeView_Add function as I want to include icons in the treeview dislpay.

So, am I right in thinking this code below could work, or could you show me how your script could be adapted to include those new functions, as mentioned before?

Func BuildExampleTree($treeID)  
    
    Local $ti, $tsi, $i, $j, $k    
$ti = _GUICtrlTreeView_Add ($treeID, 0, "Select All")   
    
     _GUICtrlTreeView_BeginUpdate ($treeID)
     For $i = 1 To 5
        $tsi = _GUICtrlTreeView_AddChild($i, $ti, "1-")
        If $i = 3 Then
            For $j = 1 To 3
                $tssi = _GUICtrlTreeView_AddChild($i & '-' & $j,$tsi, "1-")
                If $j = 2 Then
                    For $k = 1 To 4
                        _GUICtrlTreeView_AddChild($i & '-' & $j & '-' & $k, $tssi, "1-")
                    Next
                EndIf
            Next
        EndIf
    Next
    _GUICtrlTreeView_EndUpdate ($treeID)    
    _GUICtrlTreeView_Add ($treeID, 1, "2")
    _GUICtrlTreeView_Expand($treeID, 0, True)
 ;   ControlTreeView('', '', $treeID, "Expand", '#0')
  ;  ControlTreeView('', '', $treeID, "Expand", '#0|#2')
   ; ControlTreeView('', '', $treeID, "Expand", '#0|#2|#1')
EndFunc

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...