Jump to content
Sign in to follow this  
AndrewG

Read from Treeview Control

Recommended Posts

AndrewG

I'm in Treeview control hell. I have a Treeview control that gets populated with folders and the contents of each folder that are located in the script dir. I want to get the file path of a selected item. I'm using GUICtrlSetOnEvent() to call a function _Show() to read the control but no luck. I can't even get the function _Show() to execute and create a MsgBox().

Would somebody please help me out of this hell!

#include <GUIConstants.au3>
#include <Array.au3>
#include <File.au3>
#include <TreeviewConstants.au3>
#Include <GuiTreeView.au3>
#include <WindowsConstants.au3>


Opt("GUIOnEventMode", 1)
Opt("MustDeclareVars", 1)


Global $frmMain = GUICreate("Learning Treeview", 800, 480, (@DesktopWidth - 800)/2, (@DesktopHeight - 480)/2, -1, -1)
    GUISetOnEvent($GUI_EVENT_CLOSE, "ExitApp")
    
Global $frmMainFileMenu = GUICtrlCreateMenu("File")
Global $frmMainFileMenu_Exit = GUICtrlCreateMenuItem("Exit", $frmMainFileMenu)
    GUICtrlSetOnEvent($frmMainFileMenu_Exit, "ExitApp")
    
Global $frmMain_FileTree = GUICtrlCreateTreeView(10, 20, 150, 330, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS), $WS_EX_CLIENTEDGE)

Global $i, $iF, $aFiles, $aDirectorys, $aDirNames, $aFileNames
        
;Get all directorys in script directory
    $aDirectorys = _FileListToArray(@ScriptDir, '*.*', 2)
    _ArrayDelete($aDirectorys, 0)
        
;Loop through returned array and add directorys to treeview
    For $i = 0 To UBound($aDirectorys) -1
        $aDirNames = GUICtrlCreateTreeViewItem($aDirectorys[$i],$frmMain_FileTree)
            
    ;Get files in each folder
        $aFiles = _FileListToArray($aDirectorys[$i], "*", 1)
        _ArrayDelete($aFiles, 0)
        ;_ArrayDisplay($aFiles)
        
    ;Loop through returned file names and add file name to each directory
        For $iF = 0 To UBound($aFiles) -1
            $aFileNames = GUICtrlCreateTreeViewItem($aFiles[$iF],$aDirNames)
        Next
    Next


    GUICtrlSetOnEvent($frmMain_FileTree, "_Show")
    

GUISetState(@SW_SHOW)

While 1
    sleep(1000) 
WEnd

Func ExitApp()
        Exit
    EndFunc

Func _Show()
    MsgBox(0, "", GUICtrlRead($frmMain_FileTree, 0))
    ;MsgBox(0,"", _GUICtrlTreeView_GetText($frmMain_FileTree))
    ;MsgBox(0,"", $frmMain_FileTree)
    ;MsgBox(0, "", "Working")
EndFunc

Share this post


Link to post
Share on other sites
WideBoyDixon

I believe the GUICtrlSetOnEvent needs to be applied to the TreeViewItem rather than the TreeView itself. Try:

...
        $aDirNames = GUICtrlCreateTreeViewItem($aDirectorys[$i],$frmMain_FileTree)
        GUICtrlSetOnEvent($aDirNames, "_Show")
...
        $aFileNames = GUICtrlCreateTreeViewItem($aFiles[$iF],$aDirNames)
        GUICtrlSetOnEvent($aFileNames, "_Show")
...

I guess you'll also be using _GUICtrlTreeView_GetTree() ?

WBD

Share this post


Link to post
Share on other sites
Nahuel

Look at the comments I made in the code.

#include <GUIConstants.au3>
#include <Array.au3>
#include <File.au3>
#include <TreeviewConstants.au3>
#Include <GuiTreeView.au3>
#include <WindowsConstants.au3>


Opt("GUIOnEventMode", 1)
Opt("MustDeclareVars", 1)


Global $frmMain = GUICreate("Learning Treeview", 800, 480, (@DesktopWidth - 800)/2, (@DesktopHeight - 480)/2, -1, -1)
    GUISetOnEvent($GUI_EVENT_CLOSE, "ExitApp")
    
Global $frmMainFileMenu = GUICtrlCreateMenu("File")
Global $frmMainFileMenu_Exit = GUICtrlCreateMenuItem("Exit", $frmMainFileMenu)
    GUICtrlSetOnEvent($frmMainFileMenu_Exit, "ExitApp")
    
Global $frmMain_FileTree = GUICtrlCreateTreeView(10, 20, 150, 330, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS), $WS_EX_CLIENTEDGE)

Global $i, $iF, $aFiles, $aDirectorys, $aDirNames, $aFileNames
        
;Get all directorys in script directory
    $aDirectorys = _FileListToArray(@ScriptDir, '*.*', 2)
    _ArrayDelete($aDirectorys, 0)
        
;Loop through returned array and add directorys to treeview
    For $i = 0 To UBound($aDirectorys) -1
        $aDirNames = GUICtrlCreateTreeViewItem($aDirectorys[$i],$frmMain_FileTree)
         GUICtrlSetOnEvent(-1,"_Show");------>HERE   
   ;Get files in each folder
        $aFiles = _FileListToArray($aDirectorys[$i], "*", 1)
        _ArrayDelete($aFiles, 0)
       ;_ArrayDisplay($aFiles)
        
   ;Loop through returned file names and add file name to each directory
        For $iF = 0 To UBound($aFiles) -1
            $aFileNames = GUICtrlCreateTreeViewItem($aFiles[$iF],$aDirNames)
            GUICtrlSetOnEvent(-1,"_Show");------>HERE
        Next
    Next


   ;GUICtrlSetOnEvent($frmMain_FileTree, "_Show")->WRONG
    

GUISetState(@SW_SHOW)

While 1
    sleep(1000) 
WEnd

Func ExitApp()
        Exit
    EndFunc

Func _Show()
    MsgBox(0,"",GUICtrlRead(@GUI_CtrlId,1))
;MsgBox(0, "", GUICtrlRead($frmMain_FileTree, 0))
   ;MsgBox(0,"", _GUICtrlTreeView_GetText($frmMain_FileTree))
   ;MsgBox(0,"", $frmMain_FileTree)
   ;MsgBox(0, "", "Working")
EndFunc

Share this post


Link to post
Share on other sites
AndrewG

I believe the GUICtrlSetOnEvent needs to be applied to the TreeViewItem rather than the TreeView itself. Try:

...
        $aDirNames = GUICtrlCreateTreeViewItem($aDirectorys[$i],$frmMain_FileTree)
        GUICtrlSetOnEvent($aDirNames, "_Show")
...
        $aFileNames = GUICtrlCreateTreeViewItem($aFiles[$iF],$aDirNames)
        GUICtrlSetOnEvent($aFileNames, "_Show")
...

I guess you'll also be using _GUICtrlTreeView_GetTree() ?

WBD

Thank you. That Solved my issue.

I just checked out _GUICtrlTreeView_GetTree(), and yes I will be using that as well. Thanks.

Edited by AndrewG

Share this post


Link to post
Share on other sites
AndrewG

Look at the comments I made in the code.

#include <GUIConstants.au3>
#include <Array.au3>
#include <File.au3>
#include <TreeviewConstants.au3>
#Include <GuiTreeView.au3>
#include <WindowsConstants.au3>


Opt("GUIOnEventMode", 1)
Opt("MustDeclareVars", 1)


Global $frmMain = GUICreate("Learning Treeview", 800, 480, (@DesktopWidth - 800)/2, (@DesktopHeight - 480)/2, -1, -1)
    GUISetOnEvent($GUI_EVENT_CLOSE, "ExitApp")
    
Global $frmMainFileMenu = GUICtrlCreateMenu("File")
Global $frmMainFileMenu_Exit = GUICtrlCreateMenuItem("Exit", $frmMainFileMenu)
    GUICtrlSetOnEvent($frmMainFileMenu_Exit, "ExitApp")
    
Global $frmMain_FileTree = GUICtrlCreateTreeView(10, 20, 150, 330, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS), $WS_EX_CLIENTEDGE)

Global $i, $iF, $aFiles, $aDirectorys, $aDirNames, $aFileNames
        
;Get all directorys in script directory
    $aDirectorys = _FileListToArray(@ScriptDir, '*.*', 2)
    _ArrayDelete($aDirectorys, 0)
        
;Loop through returned array and add directorys to treeview
    For $i = 0 To UBound($aDirectorys) -1
        $aDirNames = GUICtrlCreateTreeViewItem($aDirectorys[$i],$frmMain_FileTree)
         GUICtrlSetOnEvent(-1,"_Show");------>HERE   
 ;Get files in each folder
        $aFiles = _FileListToArray($aDirectorys[$i], "*", 1)
        _ArrayDelete($aFiles, 0)
     ;_ArrayDisplay($aFiles)
        
 ;Loop through returned file names and add file name to each directory
        For $iF = 0 To UBound($aFiles) -1
            $aFileNames = GUICtrlCreateTreeViewItem($aFiles[$iF],$aDirNames)
            GUICtrlSetOnEvent(-1,"_Show");------>HERE
        Next
    Next


 ;GUICtrlSetOnEvent($frmMain_FileTree, "_Show")->WRONG
    

GUISetState(@SW_SHOW)

While 1
    sleep(1000) 
WEnd

Func ExitApp()
        Exit
    EndFunc

Func _Show()
    MsgBox(0,"",GUICtrlRead(@GUI_CtrlId,1))
;MsgBox(0, "", GUICtrlRead($frmMain_FileTree, 0))
 ;MsgBox(0,"", _GUICtrlTreeView_GetText($frmMain_FileTree))
 ;MsgBox(0,"", $frmMain_FileTree)
 ;MsgBox(0, "", "Working")
EndFunc

Thank you. That solved my issue.

Edited by AndrewG

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.