Jump to content
Sign in to follow this  
zamboni128

TreeView and the SpaceBar

Recommended Posts

zamboni128

I have a treeview list with checkboxes and everything is working correctly when I use the mouse to check or uncheck the item. When I use the spacebar to check or uncheck the item, it only affects the item I'm on. I need to be able to disable the spacebar or when the spacebar is pressed, call a function to set the other items checked status.

Questions:

1. Can the spacebar be disabled in a treeview?

2. Can a function be called when the user presses the spacebar in a treeview?

Thanks in advance.

Share this post


Link to post
Share on other sites
Melba23

zamboni128,

Could you please post some reproducer code - anything we code ourselves is likely to be very different and so not helpful to you. ;)

M23


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
zamboni128

I've attached my example code.

If I select 1-6 with the mouse, 1-6-1, 1-1, Parent1 and Top are all checked.

If I select 1-6 with the spacebar, only 1-6 is checked

I just realized the file didn't upload...

MyTreeViewExample.au3

Edited by zamboni128

Share this post


Link to post
Share on other sites
Melba23

zamboni128,

That was fun! ;)

#region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseX64=n
#endregion ;**** Directives created by AutoIt3Wrapper_GUI ****
;======================================================================================================================================
; Program Name:            MyTreeViewExample
; Description:          This script is a demo of the treeview function.
;
; External Programs:    None
; External Files:        None
; Author:                Robert Helgeson
;
; Asumptions:            None
;
; Revision History:
; Version 1.0:            Initial Release
; 06/15/2012 (RH)
;
;======================================================================================================================================#include <GUITreeView.au3>
#region includes
#include <Misc.au3>
#include <GUITreeView.au3>
#include <TreeViewConstants.au3>
#include <WindowsConstants.au3>
#include <GuiConstantsEX.au3>
#endregion includes

Global $hDLL = DllOpen("user32.dll")

#region Define and create Treelist
Local $hGUI = GUICreate('TreeView', 300, 700)
Local $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_CHECKBOXES)
Local $idTreeView = GUICtrlCreateTreeView(0, 0, 300, 700, $iStyle, $WS_EX_CLIENTEDGE)
Global $hTreeView = GUICtrlGetHandle($idTreeView)
_GUICtrlTreeView_BeginUpdate($hTreeView)

; Create the Treeview evtries entry
$idTreeView_Top = GUICtrlCreateTreeViewItem("Top", $idTreeView)
$hTreeView_Top = GUICtrlGetHandle($idTreeView_Top)

Local $idParent1 = GUICtrlCreateTreeViewItem('Parent1', $idTreeView_Top)
Local $hParent1 = GUICtrlGetHandle($idParent1)
$idLevel2 = GUICtrlCreateTreeViewItem('1-1', $idParent1)
For $i = 2 To 5
    GUICtrlCreateTreeViewItem('1-' & $i, $idLevel2)
Next
$idLevel5 = GUICtrlCreateTreeViewItem('1-6', $idLevel2)
GUICtrlCreateTreeViewItem('1-6-1', $idLevel5)
$idLevel3 = GUICtrlCreateTreeViewItem('2-1', $idParent1)
For $i = 2 To 5
    GUICtrlCreateTreeViewItem('2-' & $i, $idLevel3)
Next
$idLevel4 = GUICtrlCreateTreeViewItem('3-1', $idParent1)
For $i = 2 To 5
    GUICtrlCreateTreeViewItem('3-' & $i, $idLevel4)
Next
For $i = 4 To 6
    GUICtrlCreateTreeViewItem($i, $idParent1)
Next

Local $idParent2 = GUICtrlCreateTreeViewItem('Parent2', $idTreeView_Top)
Local $hParent2 = GUICtrlGetHandle($idParent2)
For $i = 1 To 5
    GUICtrlCreateTreeViewItem('2-' & $i, $idParent2)
Next
Local $idParent3 = GUICtrlCreateTreeViewItem('Parent3', $idTreeView_Top)
Local $hParent3 = GUICtrlGetHandle($idParent3)
For $i = 1 To 5
    GUICtrlCreateTreeViewItem('3-' & $i, $idParent3)
Next
Local $idParent4 = GUICtrlCreateTreeViewItem('Parent4', $idTreeView_Top)
Local $hParent4 = GUICtrlGetHandle($idParent4)

; Expand all parents on startup
_GUICtrlTreeView_SetState($hTreeView, $hTreeView_Top, $TVIS_EXPANDED)
_GUICtrlTreeView_SetState($hTreeView, $hParent1, $TVIS_EXPANDED)
_GUICtrlTreeView_SetState($hTreeView, GUICtrlGetHandle($idLevel5), $TVIS_EXPANDED)
_GUICtrlTreeView_SetState($hTreeView, $hParent2, $TVIS_EXPANDED)
_GUICtrlTreeView_SetState($hTreeView, GUICtrlGetHandle($idLevel2), $TVIS_EXPANDED)
_GUICtrlTreeView_SetState($hTreeView, GUICtrlGetHandle($idLevel3), $TVIS_EXPANDED)
_GUICtrlTreeView_SetState($hTreeView, GUICtrlGetHandle($idLevel4), $TVIS_EXPANDED)
_GUICtrlTreeView_SetState($hTreeView, $hParent3, $TVIS_EXPANDED)
_GUICtrlTreeView_SetState($hTreeView, $hParent4, $TVIS_EXPANDED)

_GUICtrlTreeView_EndUpdate($hTreeView)

GUISetState()

#endregion Define and create Treelist
#region Program Control
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            DllClose($hDLL)
            _GUICtrlTreeView_Destroy($hTreeView)
            GUIDelete()
            Exit
        Case $msg = $GUI_EVENT_PRIMARYDOWN
            _Test_Check()
    EndSelect
    If _IsPressed("20", $hDLL) Then
        _Test_Check()
        While _IsPressed("20", $hDLL)
            Sleep(10)
        WEnd
    EndIf
WEnd
#endregion Program Control

#region Functions
Func _Test_Check()

    $hItem = _GUICtrlTreeView_GetSelection($hTreeView)
    ConsoleWrite(_GUICtrlTreeView_GetSelected($hTreeView, $hItem) & @CRLF)
    Switch _GUICtrlTreeView_GetChecked($hTreeView, $hItem)
        Case True
            ConsoleWrite("Checked" & @CRLF)
            $fChecked = True
        Case False
            ConsoleWrite("Not Checked" & @CRLF)
            $fChecked = False
    EndSwitch
    _TvCheckbox($hItem, $fChecked)
EndFunc

Func _TvCheckbox($hTvItem, $fTvCheck)
    ;-------------- Going up the tree -----------------------------------
    _TvCheckboxParent($hTvItem, $fTvCheck)

    ;-------------- Going down the tree -----------------------------------
    _TvCheckboxChild($hTvItem, $fTvCheck)
EndFunc   ;==>_TvCheckbox

Func _TvCheckboxChild($hTvItem, $fTvCheck)
    Local $hFirst, $hNext
    $hFirst = _GUICtrlTreeView_GetFirstChild($hTreeView, $hTvItem)
    If $hFirst Then
        $hNext = $hFirst
        While $hNext
            _GUICtrlTreeView_SetChecked($hTreeView, $hNext, $fTvCheck)
            If _GUICtrlTreeView_GetFirstChild($hTreeView, $hNext) Then
                _TvCheckboxChild($hNext, $fTvCheck)
            EndIf
            $hNext = _GUICtrlTreeView_GetNextSibling($hTreeView, $hNext)
        WEnd
    EndIf
EndFunc   ;==>_TvCheckboxChild

Func _TvCheckboxParent($hTvItem, $fTvCheck)
    Local $hFirst, $hNext, $bCheckedFlag

    If $fTvCheck Then
        $hTvParent = _GUICtrlTreeView_GetParentHandle($hTreeView, $hTvItem)
        If $hTvParent = 0 Then
            _GUICtrlTreeView_SetChecked($hTreeView, $hTvItem, $fTvCheck)
        Else
            $hFirst = _GUICtrlTreeView_GetParentHandle($hTreeView, $hTvItem)
            If $hFirst Then
                $hNext = $hFirst
                _GUICtrlTreeView_SetChecked($hTreeView, $hNext, $fTvCheck)
                If _GUICtrlTreeView_GetParentHandle($hTreeView, $hNext) Then
                    _TvCheckboxParent($hNext, $fTvCheck)
                EndIf
            EndIf
        EndIf
    Else
        $hTvParent = _GUICtrlTreeView_GetParentHandle($hTreeView, $hTvItem)
        If $hTvParent = 0 Then ; reached the top of the list
            _GUICtrlTreeView_SetChecked($hTreeView, $hTvItem, $fTvCheck)
        Else
            $hNext = $hTvParent
            ; check to see if any siblings are checked
            $hSibling = _GUICtrlTreeView_GetFirstChild($hTreeView, $hNext)
            $bCheckedFlag = True
            While $hSibling > 0
                If _GUICtrlTreeView_GetChecked($hTreeView, $hSibling) Then
                    $bCheckedFlag = False
                    ExitLoop
                EndIf
                $hSibling = _GUICtrlTreeView_GetNextSibling($hTreeView, $hSibling)
            WEnd
            If $bCheckedFlag Then
                _GUICtrlTreeView_SetChecked($hTreeView, $hNext, $fTvCheck)
            EndIf
            If _GUICtrlTreeView_GetParentHandle($hTreeView, $hNext) Then
                _TvCheckboxParent($hNext, $fTvCheck)
            EndIf
        EndIf
    EndIf
EndFunc   ;==>_TvCheckboxParent

#endregion Functions

M23


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
zamboni128

Thanks Melba23

That worked perfect. Thank you for responding so quickly.

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
Sign in to follow this  

  • Similar Content

    • xtcislove
      By xtcislove
      Hello,
      i stuck again,
      Im using this function to create a treeview from root dir.
       
      ;https://autoit.de/index.php?thread/86082-treeview-root-verbergen/&postID=691139#post691139 #include <File.au3> #include <WindowsConstants.au3> Global $sPath = @ScriptDir Global $hGui = GUICreate('TreeView-Example', 400, 600) Global $idTreeView = GUICtrlCreateTreeView(10, 10, 380, 580, Default, $WS_EX_CLIENTEDGE) GUISetState() _CreatePath($sPath, $idTreeView) Do Until GUIGetMsg() = -3 Func _CreatePath($sPath, $idParent) Local $aFolder, $aFiles, $idItem If StringRight($sPath, 1) <> '\' Then $sPath &= '\' $aFolder = _FileListToArray($sPath, '*', $FLTA_FOLDERS) If Not @error Then For $i = 1 To $aFolder[0] $idItem = GUICtrlCreateTreeViewItem($aFolder[$i], $idParent) _CreatePath($sPath & $aFolder[$i], $idItem) Next EndIf $aFiles = _FileListToArray($sPath, '*', $FLTA_FILES) If @error Then Return For $i = 1 To $aFiles[0] $idItem = GUICtrlCreateTreeViewItem($aFiles[$i], $idParent) Next EndFunc Folder Structure:

      Folder1
      Folder2
      Folder3
      If a file exists in multiple folders, i like to color it red, if not green. 

      I know how to do this for files, but nut for the folders.

      Because if there is only 1 file in Folder2 that is also in Folder1 that it should only color this single file red, inlcuding its whole tree.

      The Folder1 and Folder2 should be red in this case, too. Other files and trees should stay green.
       
      Edit:
      Basically i like to color a file and its belonging tree red if the file exists more than 1 time.
    • xtcislove
      By xtcislove
      Hello,

      im searching like 6 hours and i didnt found the right solution.

      Im trying to build a treeview from a directory and its subfolders etc.

      This function give me right treeview, but i cant color each item seperated.

       
      #include <GuiTreeView.au3> $hGui = GUICreate("Demo1", 600, 400) $hTreeView = _GUICtrlTreeView_Create($hGui, 10, 10, 580, 380) GUISetState() _GUICtrlTreeView_BeginUpdate($hTreeView) ListFiles_ToTreeView(@ScriptDir, 0) _GUICtrlTreeView_EndUpdate($hTreeView) Do Until GUIGetMsg() = -3 Func ListFiles_ToTreeView($sSourceFolder, $hItem) Local $sFile ; Force a trailing \ If StringRight($sSourceFolder, 1) <> "\" Then $sSourceFolder &= "\" ; Start the search Local $hSearch = FileFindFirstFile($sSourceFolder & "*.*") ; If no files found then return If $hSearch = -1 Then Return ; This is where we break the recursive loop <<<<<<<<<<<<<<<<<<<<<<<<<< ; Now run through the contents of the folder While 1 ; Get next match $sFile = FileFindNextFile($hSearch) ; If no more files then close search handle and return If @error Then ExitLoop ; This is where we break the recursive loop <<<<<<<<<<<<<<<<<<<<<<<<<< ; Check if a folder If @extended Then ; If so then call the function recursively ListFiles_ToTreeView($sSourceFolder & $sFile, _GUICtrlTreeView_AddChild($hTreeView, $hItem, $sFile)) Else ; If a file than write path and name _GUICtrlTreeView_AddChild($hTreeView, $hItem, $sFile) EndIf WEnd ; Close search handle FileClose($hSearch) EndFunc ;==>ListFiles_ToTreeView
      So i am searching for the exact same function just with
      GUICtrlCreateTreeViewItem
      instead of 
      _GUICtrlTreeView_AddChild
       
      Ps: i have a folder structure where i like to color each item green if a file only exist once and red if it exist more than once.

      Could someone help?
       
      Solution:

       
       
    • Sven-Seyfert
      By Sven-Seyfert
       
      Hi Community,

      I have a problem with the usage of _GUICtrlTreeView_AddChild and setting different item text colors (and different background colors), separately for the TreeView levels.

      I use the UDF GUITreeViewEx.au3 of 'Melba23' and I renamed the functions to increase my understanding about the content. I already talked to him about his UDF to respect his rights and intellectual property. I read many forum threads, I tried many ways to get it, but it's seems to be impossible in the way that I use the UDF.

      Question:
      How can I set different item text colors, separately for the TreeView levels?
      How can I set different item background colors, separately for the TreeView levels?

      I found out that _GUICtrlTreeView_AddChild don't let my set this properties directly. But after loading the TreeView content I can't change the items (children) too. Only the font weight to bold I could set with _GUICtrlTreeView_SetBold but I want to do this in a generic way directly at the creation (loading) of the TreeView.

      Of course I tried the different functions to set text color or bkgColor, but it didn't work - maybe I do it wrong.
      I really hope you can help me, give me a hint and understand what I want to do.
      [Solved] final solution made by @LarsJ (see below)

      Thanks for your help - I'm grateful!
      Sven

      CODE
      BEFORE
      AFTER
      ExecutionPlan.zip
    • gillesg
      By gillesg
      Hello,
      I am struggling in merging GUITreeViewEx, Shelltristate and enhancing to handle a third state that means : some items under are selected.
      I have difficulties handling expand order and key Space (especially when node is collapsed).
      Here the zip with UDF and and example.
       
      The problem I might need some advice to handle : 
      1- When load Treeview, have a correct settings of the checkbox for a tristate tree
      2 - Handle keyboard used for walking in tree
           Chicken is checked and  Steak is unchecked
          When walking with arrow to Meat, it gets unchecked
      3 - When node is collapsed and checked thru keyboard (space)
         the middle state is possible which should not
      Here is joined an animated gif showing the 3 problems
       
      Thanks for your advices
       
       
       
       
       
       
       
       
       
       
       

      GUITreeview3Ex.zip
    • gillesg
      By gillesg
      Hello,
      I just bumped into this problem with _GUICtrlTreeView_Sort.
      Just used doc example and comment out the child creation
      #include <GUIConstantsEx.au3> #include <GuiTreeView.au3> #include <MsgBoxConstants.au3> #include <WindowsConstants.au3> Example() Func Example() Local $aidItem[10], $iX = 9, $iY = 29, $idTreeView Local $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS) GUICreate("TreeView Sort", 400, 300) $idTreeView = GUICtrlCreateTreeView(2, 2, 396, 268, $iStyle, $WS_EX_CLIENTEDGE) GUISetState(@SW_SHOW) _GUICtrlTreeView_BeginUpdate($idTreeView) For $x = 0 To 3 $aidItem[$x] = GUICtrlCreateTreeViewItem(StringFormat("[%02d] New Item", $iX), $idTreeView) $iX -= 1 For $y = 1 To 3 ; GUICtrlCreateTreeViewItem(StringFormat("[%02d] New Child", $iY), $aidItem[$x]) $iY -= 1 Next Next _GUICtrlTreeView_Expand($idTreeView) _GUICtrlTreeView_EndUpdate($idTreeView) MsgBox($MB_SYSTEMMODAL, "Information", "Sort") _GUICtrlTreeView_Sort($idTreeView) _GUICtrlTreeView_SelectItem($idTreeView, $aidItem[9]) ; Boucle jusqu'à ce que l'utilisateur quitte. Do Until GUIGetMsg() = $GUI_EVENT_CLOSE GUIDelete() EndFunc ;==>Example The tree is not sorted.
       
      There is a similar problem with deeper tree
      Also based on the example
      #include <GUIConstantsEx.au3> #include <GuiTreeView.au3> #include <MsgBoxConstants.au3> #include <WindowsConstants.au3> Example() Func Example() Local $aidItem[10], $bidItem[10], $iX = 9, $iY = 29, $idTreeView Local $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS) GUICreate("TreeView Sort", 400, 300) $idTreeView = GUICtrlCreateTreeView(2, 2, 396, 268, $iStyle, $WS_EX_CLIENTEDGE) GUISetState(@SW_SHOW) _GUICtrlTreeView_BeginUpdate($idTreeView) $index=0 For $x = 0 To 3 $aidItem[$x] = GUICtrlCreateTreeViewItem(StringFormat("[%02d] New Item", $iX), $idTreeView) $iX -= 1 For $y = 1 To 2 $BidItem[$y]= GUICtrlCreateTreeViewItem(StringFormat("[%02d] New Child", $iY), $aidItem[$x]) $iY -= 1 For $z= 1 To 2 GUICtrlCreateTreeViewItem(StringFormat("[%02d] New Child", $iY), $BidItem[$y]) $iY -= 1 Next Next Next _GUICtrlTreeView_Expand($idTreeView) _GUICtrlTreeView_EndUpdate($idTreeView) MsgBox($MB_SYSTEMMODAL, "Information", "Sort") _GUICtrlTreeView_Sort($idTreeView) _GUICtrlTreeView_SelectItem($idTreeView, $aidItem[9]) ; Boucle jusqu'à ce que l'utilisateur quitte. Do Until GUIGetMsg() = $GUI_EVENT_CLOSE GUIDelete() EndFunc ;==>Example The tree is now 2 level deep and it is not properly sorted.
       
      Any advices ?
       
      Regards
×