Sign in to follow this  
Followers 0
zamboni128

TreeView and the SpaceBar

5 posts in this topic

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



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


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

#3 ·  Posted (edited)

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

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


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

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  
Followers 0

  • Similar Content

    • corgano
      By corgano
      I can't explain easily what i want to do, so i made a picture:

      What i want to do is to specify a treeview item, and then be able to move it right to be a child of the previous item, or left to do the opposite (make a child into a sibling). I also want to move and child items of the target with it, like in the bottom half of the above image depicts. However I didn't see any functions in the help file to move items around. All i want is a button that when pressed, makes the selected item and any of selected item's children move one layer to the right / left. Does anyone know how?

      Edit: something along the lines of _GUICtrlTreeView_Level but SET level
    • Tjalve
      By Tjalve
      Hi everyone. Im creating a GUI for an application. Im usinga  treeview item and i want to add some information to the side of the treeview based on what cilditem you are pressing. For the moment i have a "get info" button that works. But that means that you have to select your child in the treeview, and then press a button to gte the info. I want the info to be visable as soon as you select it.
      With uther controls you can just add it to the loop but it doesnt seem to work with tyhe UDF version of the treeview control. Is there anything I can do?
      #include <GUIConstantsEx.au3> #include <GuiTreeView.au3> #include <MsgBoxConstants.au3> #include <WindowsConstants.au3> ;~ Create GUI dim $guilabel[7] local $currentlevel = 1 Local $hGui = GUICreate( "Grejs", 1000, 500, 600, 300, BitOR( $GUI_SS_DEFAULT_GUI, $WS_MAXIMIZEBOX, $WS_SIZEBOX ) ) Local $iBorderWidth = 4, $aPos = WinGetClientSize( $hGui ) $idTV = _GUICtrlTreeView_Create($hGui, $iBorderWidth, $iBorderWidth, $aPos[0]-2*$iBorderWidth-400, $aPos[1]-2*$iBorderWidth-30 ) $root_level = _GUICtrlTreeView_Add($idTV,0,"Root") $item1 = _GUICtrlTreeView_AddChild($idTV, $root_level, "child1") $item2 = _GUICtrlTreeView_AddChild($idTV, $root_level, "child2") $item3 = _GUICtrlTreeView_AddChild($idTV, $root_level, "child3") ;~ Start Gui GUISetState() While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop Case $idTV MsgBox(0,"","") EndSwitch WEnd GUIDelete($hGui) Exit In this example, i want the msgbox to appear whenevr i select something in the treeview. It ios not my full code, but its an example of the problam i have. Help is much appritiated
      /G
    • Trolleule
      By Trolleule
      Hey,
      i'm building a "Jump To" registry key function and trying to get access to the regedit treeview control like this:
      #include <GUIConstants.au3> #include <GuiTreeView.au3> #include <TreeViewConstants.au3> #include <SendMessage.au3> #include <WinAPI.au3> Global $h_item ShellExecute("regedit.exe") ;~ Local $pid = ProcessExists("regedit.exe") ;~ If $pid Then ;~ $h_tree = WinHandFromPID($pid, "[CLASS:RegEdit_RegEdit]") ;~ WinActivate("[CLASS:RegEdit_RegEdit]", "") ;~ EndIf WinWaitActive("[CLASS:RegEdit_RegEdit]", "") ;~ WinActivate("[CLASS:RegEdit_RegEdit]", "") ;~ WinSetState("[CLASS:RegEdit_RegEdit]", "", @SW_SHOW) ; activate ;~ WinSetState("[CLASS:RegEdit_RegEdit]", "", @SW_RESTORE) ; activate ;~ WinSetState("[CLASS:RegEdit_RegEdit]", "", @SW_RESTORE) ; activate $h_tree = WinGetHandle("[CLASS:RegEdit_RegEdit]", "") ;~ $ret = _WinAPI_ShowWindow($h_tree, @SW_RESTORE) $ctrl = ControlGetHandle($h_tree, "", "[CLASS:SysTreeView32]") ConsoleWrite("$h_tree: " & $h_tree & @CRLF) ConsoleWrite("$ctrl: " & $ctrl & @CRLF) $show = GUICtrlCreateButton("Show", 70, 180, 70, 20) Show() ;~ While 1 ;~ Sleep(200) ;~ WEnd Func Show() $result = "" _SendMessage($ctrl, $WM_SETFOCUS, 0, 0) If @error Then ConsoleWrite("error" & @CRLF) $h_item = _SendMessage($ctrl, $TVM_GETNEXTITEM, $TVGN_ROOT, 0) $split = StringSplit("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders", "\") For $i=1 To $split[0] ConsoleWrite("item bevor findkey: " & $h_item & @CRLF) $h_item = FindKey($h_item, $split[$i]) If $h_item = 0 Then Return _SendMessage($ctrl, $TVM_SELECTITEM, 0x0009, $h_item) ; expand tree _GUICtrlTreeView_Expand($ctrl, $h_item) Next MsgBox(0, "dsfsdf", "sdfsf") Exit EndFunc Func FindKey($hItemParent, $sKey) $h_item = _SendMessage($ctrl, $TVM_GETNEXTITEM, $TVGN_CHILD, $hItemParent) ConsoleWrite("child : " & $h_item & " key: " & $sKey & " parent: " & $hItemParent & " parent text: " & _GUICtrlTreeView_GetText($ctrl, $hItemParent) & @CRLF) While $h_item <> 0 Local $sItemText = _GUICtrlTreeView_GetText($ctrl, $h_item) ConsoleWrite("text : " & $sItemText & @CRLF) If $sItemText = $sKey Then Return $h_item $h_item = _SendMessage($ctrl, $TVM_GETNEXTITEM, $TVGN_NEXT, $h_item) ConsoleWrite("next : " & $h_item & @CRLF) WEnd ConsoleWrite("not found" & @CRLF) Return 0 EndFunc it's working, when i start regedit.exe within my app. But can't get it work when regedit.exe already open. Some ideas?
    • tarretarretarre
      By tarretarretarre
      So I am trying to create a file tree structure Using _FileListToArrayRec with GuiCtrlCreateTreeView. But i have some problems with levels of directorys. In my mind there is nothing wrong with my logic, but obviously it is, since i dont get the output i want  
       
      Here is a printscreen of the outcome, the array is sorted so it feels like it should be pretty easy, but its not
       
      Here is the working code
      #include <File.au3> #include <Array.au3> #include <GUITreeview.au3> #include <WINapiSHELLEX.au3> Local $hGI = GUICreate("test", 500,500) Local $hTreeView = GUICtrlCreateTreeView(0,0,500,500) GUISetState() Local $arr = _GuictrlTreeview__FileListToArrayRec($hTreeView,"C:\Autoit\REMOVE", "*.au3||BackUp;.git*;___TrashCan") _ArrayDisplay($arr) Func _GuictrlTreeview__FileListToArrayRec($hTreeView, $sPath, $sFilter) if StringRight($sPath,1) == "\" Then $sPath = StringLeft($sPath, StringLen($sPath) - 1) if not FileExists($sPath) Then Return SetError(1,0,0) Local $aFileList = _FileListToArrayRec($sPath, $sFilter, $FLTAR_FILESFOLDERS, $FLTAR_RECUR, $FLTAR_SORT, $FLTAR_FULLPATH) if @error Then Return SetError(1,0,0) Local $aParents[1] = [0] Local $sLastParentDirectory = False Local $hLastParentTreeItem = False Local $hLastParentChildItem = False ; Set default icons (late rreplaced with file associated icon $hImageList = _GUIImageList_Create(16, 16, 5, 1) _GUICtrlTreeView_SetNormalImageList($hTreeView, $hImageList) ; Begin tree update _GUICtrlTreeView_BeginUpdate($hTreeView) For $i = 1 To $aFileList[0] Local $sCurFullFilePath = $aFileList[$i] Local $sCurFileName = _Misc_GetFileName($aFileList[$i], False) ;Get da icon Local $hIcon = _WinAPI_ShellExtractAssociatedIcon($sCurFullFilePath, 1) Local $hIcon_Index = _GUIImageList_ReplaceIcon($hImageList, -1, $hIcon) ;If directory If FileGetAttrib($sCurFullFilePath) == "D" Then ; Check if sub has file to add If StringLeft($sCurFullFilePath, StringLen($sLastParentDirectory)) == $sLastParentDirectory Then $hLastParentChildItem = _GUICtrlTreeView_AddChild($hTreeView, $hLastParentTreeItem, $sCurFileName, $hIcon_Index, $hIcon_Index) ;Loop will end here ContinueLoop EndIf ;Create new parent folder & Update parents $sLastParentDirectory = $sCurFullFilePath $hLastParentTreeItem = _GUICtrlTreeView_Add($hTreeView, 0, StringFormat("%s", $sCurFileName), $hIcon_Index, $hIcon_Index) Else If Not $hLastParentChildItem Then ;ConsoleWrite($hLastParentTreeItem&" "&$sCurFileName&@CRLF) _GUICtrlTreeView_AddChild($hTreeView, $hLastParentTreeItem, $sCurFileName, $hIcon_Index, $hIcon_Index) Else _GUICtrlTreeView_AddChild($hTreeView, $hLastParentChildItem, $sCurFileName, $hIcon_Index, $hIcon_Index) EndIf EndIf Next ; End update _GUICtrlTreeView_EndUpdate($hTreeView) Return $aFileList EndFunc Func _Misc_GetFileName($sFilePath, $KeepExtension = True) ; Gets the filename Local $StringSPlit = StringSplit($sFilePath, "\") If Not $KeepExtension Then Return StringRegExpReplace($StringSPlit[$StringSPlit[0]], "\.(.*?)[a-zA-Z1234567890_-]{1,}+", "") Return $StringSPlit[$StringSPlit[0]] EndFunc ;==>_Misc_GetFileName  
      Any tips ?
       
      Thanks in advance
    • Kanashius
      By Kanashius
      This UDF can be used to make a Treeview and/or Listview able to browse Files.
      If you like it, please leave me a comment, also if you have any suggestions to make it better or if you found bugs.
      FileExplorerTreeAndListview_source.zip