Jump to content

Read selected item in Treeview - FileManager


Recommended Posts

Hi, 

i am trying to create a FileManager, now i know there are better programs out there to do it, but i think its fun to try and learn nu things.

Now i am getting stuck in my code i created a button to select a driver/folder after that i put's in the driver/folder contant in the TreeView, but when i select something and click on my button copy.

It returns a 0 instead of something else, when i add a value to the TreeView with: GuiCtrlCreateTreeViewItem and i select that one, then my copy button works.

Hope you guys can help me find out/point in the right direction where i am going wrong with this. Any pointers to a nicer GUI or a cleaner script  would be appreciated :)

Below find my code:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiStatusBar.au3>
#include <ProgressConstants.au3>
#include <TreeViewConstants.au3>
#include <WindowsConstants.au3>
#include <GuiTreeView.au3>
;-------------------------------------------------------------------------------------------------------------------------------------------
#NoTrayIcon
;-------------------------------------------------------------------------------------------------------------------------------------------
If @OSVersion   = "WIN_XP" Then
    $OS         = "Windows XP"
EndIf
If @OSVersion   = "WIN_VISTA" Then
    $OS         = "Windows Vista"
EndIf
If @OSVersion   = "WIN_7" Then
    $OS         = "Windows 7"
EndIf
If @OSVersion   = "WIN_8" Then
    $OS         = "Windows 8"
EndIf
If @OSVersion   = "WIN_81" Then
    $OS         = "Windows 8.1"
EndIf
;-------------------------------------------------------------------------------------------------------------------------------------------
$Form1          = GUICreate("", 615, 470, -1, -1, $WS_POPUPWINDOW)
$TITLE = GUICtrlCreateLabel("FileManager", 256, 8, 103, 24)
    GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
    GUICtrlSetState($TITLE, $GUI_DISABLE)
$INFO           = GUICtrlCreateLabel("Hostname:" & @TAB & @ComputerName & @CRLF & "OS:" & @TAB & @TAB & $OS, 16, 437, 200, 60)
    GUICtrlSetState($INFO, $GUI_DISABLE)

GUICtrlCreateLabel("Source", 16, 16)
$SOURCEPROGRESS = GUICtrlCreateProgress(16, 72, 289, 17, $PBS_SMOOTH)
$SOURCETREE     = GUICtrlCreateTreeView(16, 104, 289, 305, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS), $WS_EX_CLIENTEDGE)
    GUICtrlCreateTreeViewItem("Test1", $SOURCETREE)
$SOURCEINPUT    = GUICtrlCreateInput("", 16, 40, 121, 21)
    GUICtrlSetState($SOURCEINPUT, $GUI_DISABLE)
$SELECTSOURCE   = GUICtrlCreateButton("...", 144, 42, 43, 17)


GUICtrlCreateLabel("Target", 568, 16)
$TARGETPROGRESS = GUICtrlCreateProgress(312, 72, 289, 17, $PBS_SMOOTH)
$TARGETTREE     = GUICtrlCreateTreeView(312, 104, 289, 305, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS), $WS_EX_CLIENTEDGE)
$TARGETINPUT    = GUICtrlCreateInput("", 480, 40, 121, 21)
    GUICtrlSetState($TARGETINPUT, $GUI_DISABLE)
$SELECTTARGET   = GUICtrlCreateButton("...", 432, 42, 43, 17)

$COPY           = GUICtrlCreateButton("Copy", 182, 440, 75, 25)
$MOVE           = GUICtrlCreateButton("Move", 270, 440, 75, 25)
$DELETE         = GUICtrlCreateButton("Delete", 358, 440, 75, 25)
$RENAME         = GUICtrlCreateButton("Rename", 446, 440, 75, 25)
$CLOSE          = GUICtrlCreateButton("Exit", 534, 440, 75 , 25)

GUISetState(@SW_SHOW)
;-------------------------------------------------------------------------------------------------------------------------------------------
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $CLOSE
            Exit
        Case $SELECTSOURCE
            SELECTSOURCE()
        Case $SELECTTARGET
            SELECTTARGET()
        Case $COPY
            COPY()
    EndSwitch
WEnd
;-------------------------------------------------------------------------------------------------------------------------------------------
Func SELECTSOURCE()
    $VAR = FileSelectFolder("Select Source drive/folder", "")
        GUICtrlSetData($SOURCEINPUT, $VAR)

Local $iWait    = 10
Local $iSavPos  = 0

        Do
            For $i = $iSavPos To 100
                GUICtrlSetData($SOURCEPROGRESS, $i)
                Sleep($iWait)
                $iSavPos = $i
            Next
        Until $iSavPos = 100

    _GUICtrlTreeView_BeginUpdate($SOURCETREE)
        ListFiles_FolderSOURCE($VAR & "\", 0)
    _GUICtrlTreeView_EndUpdate($SOURCETREE)

EndFunc
;-------------------------------------------------------------------------------------------------------------------------------------------
Func SELECTTARGET()
    $VAR = FileSelectFolder("Select Source drive/folder", "")
        GUICtrlSetData($TARGETINPUT, $VAR)

Local $iWait    = 10
Local $iSavPos  = 0

        Do
            For $i = $iSavPos To 100
                GUICtrlSetData($TARGETPROGRESS, $i)
                Sleep($iWait)
                $iSavPos = $i
            Next
        Until $iSavPos = 100

    _GUICtrlTreeView_BeginUpdate($TARGETTREE)
        ListFiles_FolderTARGET($VAR & "\", 0)
    _GUICtrlTreeView_EndUpdate($TARGETTREE)

EndFunc
;-------------------------------------------------------------------------------------------------------------------------------------------
Func ListFiles_FolderSOURCE($SourceFolder, $Item)
        Local $File
            If StringRight($SourceFolder, 1) <> "" Then $SourceFolder &= ""
        Local $Search = FileFindFirstFile($SourceFolder & "*.*")
            If $Search = -1 Then Return

    While 1
        $File = FileFindNextFile($Search)
        If @error Then ExitLoop
        If @extended Then
        ListFiles_FolderSOURCE($SourceFolder & $File, _GUICtrlTreeView_AddChild($SOURCETREE, $Item, $File))
    Else
        _GUICtrlTreeView_AddChild($SOURCETREE, $Item, $File)
        EndIf
    WEnd
    FileClose($Search)
EndFunc
;-------------------------------------------------------------------------------------------------------------------------------------------
Func ListFiles_FolderTARGET($TargetFolder, $Item)
        Local $File
            If StringRight($TargetFolder, 1) <> "" Then $TargetFolder &= ""
        Local $Search = FileFindFirstFile($TargetFolder & "*.*")
            If $Search = -1 Then Return

    While 1
        $File = FileFindNextFile($Search)
        If @error Then ExitLoop
        If @extended Then
        ListFiles_FolderTARGET($TargetFolder & $File, _GUICtrlTreeView_AddChild($TARGETTREE, $Item, $File))
    Else
        _GUICtrlTreeView_AddChild($TARGETTREE, $Item, $File)
        EndIf
    WEnd
    FileClose($Search)
EndFunc
;-------------------------------------------------------------------------------------------------------------------------------------------
Func COPY()
    $VAR = GUICtrlRead($SOURCETREE)
        If $VAR = 0 Then
                    MsgBox($MB_SYSTEMMODAL, "Warning!", "No item currently selected")
        Else
            $sText = GUICtrlRead($VAR, 1) ; Get the text of the treeview item
            If $sText == "" Then
                MsgBox($MB_SYSTEMMODAL, "Warning!", "Error while retrieving info about selected item")
            Else
                MsgBox($MB_SYSTEMMODAL, "FileManager", "Current item selected is: " & $sText)
            EndIf
        EndIf
EndFunc
;-------------------------------------------------------------------------------------------------------------------------------------------
;-------------------------------------------------------------------------------------------------------------------------------------------
Link to comment
Share on other sites

When you create a TV-item with GUICtrlCreateTreeViewItem, the AutoIt ControlID is stored in a field called ItemParam in an internal data structure of the TV-item. When you read the selected TV-item with GUICtrlRead, ControlID from ItemParam is returned.

When you add a TV-item with _GUICtrlTreeView_AddChild or similar commands in the UDF, no ControlID is stored in ItemParam. ItemParam contains the value zero. When you read the TV-item with GUICtrlRead it returns zero.

If you are not aware of this difference between native AutoIt commands and commands from the UDF, you should not mix up the two sets of commands.

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

×
×
  • Create New...