Sign in to follow this  
Followers 0
Wingens

Read selected item in Treeview - FileManager

2 posts in this topic

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
;-------------------------------------------------------------------------------------------------------------------------------------------
;-------------------------------------------------------------------------------------------------------------------------------------------

Share this post


Link to post
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.

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

    • GordonFreeman
      By GordonFreeman
      Hi, i searched some topics but cannot found a simple way to do that:
      #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> $Form1 = GUICreate("Form1", 615, 438, 192, 124) GUISetState(@SW_SHOW) ; I tried this but user reported that it work in 2006, not seems work actually $filemenu = GUICtrlCreateMenu ("&File") $fileitem = GUICtrlCreateMenuitem ("Open",$filemenu) GuiCtrlSetImage($fileitem, "shell32.dll", 4) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd Also tried:
      #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> $Form1 = GUICreate("Form1", 615, 438, 192, 124) GUISetState(@SW_SHOW) $filemenu = TrayCreateMenu("&File") $fileitem = TrayCreateItem("Open",$filemenu) ;GuiCtrlSetImage($fileitem, "shell32.dll", 4) ;GuiCtrlSetImage(TrayItemGetHandle($fileitem), "shell32.dll", 4) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd Well.. thanks in advance!
       
      Ok. I found this working example from Yashied. If i cannot find a simple then i will go with it
      #Include <GUIConstantsEx.au3> #Include <GUIMenu.au3> #Include <Constants.au3> #Include <WinAPI.au3> #Include <WindowsConstants.au3> Opt('MustDeclareVars', 1) Global $hMenu, $hForm, $hFile = 1000, $idNew, $idExit $hForm = GUICreate('Menu', 400, 300) $hFile = _GUICtrlMenu_CreateMenu() _GUICtrlMenu_InsertMenuItem ($hFile, 0, ' &Favorites', $idNew) _GUICtrlMenu_InsertMenuItem ($hFile, 1, '', 0) _GUICtrlMenu_InsertMenuItem($hFile, 2, ' E&xit', $idExit) $hMenu = _GUICtrlMenu_CreateMenu() _GUICtrlMenu_InsertMenuItem($hMenu, 0, '&File', 0, $hFile) _GUICtrlMenu_SetMenu($hForm, $hMenu) _GUICtrlMenu_SetItemBmp($hFile, 0, _CreateBitmapFromIcon(_WinAPI_GetSysColor($COLOR_MENU), @SystemDir & '\shell32.dll', 43, 16, 16)) _GUICtrlMenu_SetItemBmp($hFile, 2, _CreateBitmapFromIcon(_WinAPI_GetSysColor($COLOR_MENU), @SystemDir & '\shell32.dll', 27, 16, 16)) GUISetState() Do Until GUIGetMsg() = $GUI_EVENT_CLOSE Func _CreateBitmapFromIcon($iBackground, $sIcon, $iIndex, $iWidth, $iHeight) Local $hDC, $hBackDC, $hBackSv, $hIcon, $hBitmap $hDC = _WinAPI_GetDC(0) $hBackDC = _WinAPI_CreateCompatibleDC($hDC) $hBitmap = _WinAPI_CreateSolidBitmap(0, $iBackground, $iWidth, $iHeight) $hBackSv = _WinAPI_SelectObject($hBackDC, $hBitmap) $hIcon = _WinAPI_PrivateExtractIcon($sIcon, $iIndex, $iWidth, $iHeight) If Not @error Then _WinAPI_DrawIconEx($hBackDC, 0, 0, $hIcon, 0, 0, 0, 0, $DI_NORMAL) _WinAPI_DestroyIcon($hIcon) EndIf _WinAPI_SelectObject($hBackDC, $hBackSv) _WinAPI_ReleaseDC(0, $hDC) _WinAPI_DeleteDC($hBackDC) Return $hBitmap EndFunc ;==>_CreateBitmapFromIcon Func _WinAPI_PrivateExtractIcon($sIcon, $iIndex, $iWidth, $iHeight) Local $hIcon, $tIcon = DllStructCreate('hwnd'), $tID = DllStructCreate('hwnd') Local $Ret = DllCall('user32.dll', 'int', 'PrivateExtractIcons', 'str', $sIcon, 'int', $iIndex, 'int', $iWidth, 'int', $iHeight, 'ptr', DllStructGetPtr($tIcon), 'ptr', DllStructGetPtr($tID), 'int', 1, 'int', 0) If (@error) Or ($Ret[0] = 0) Then Return SetError(1, 0, 0) EndIf $hIcon = DllStructGetData($tIcon, 1) If ($hIcon = Ptr(0)) Or (Not IsPtr($hIcon)) Then Return SetError(1, 0, 0) EndIf Return $hIcon EndFunc ;==>_WinAPI_PrivateExtractIcon Ok, i noticed that is a pain create submenu with _GUICtrlMenu_CreatePopup to do like in Tray, and Tray better located, then i need images/icon with Tray functions, some workaround?? 
    • nacerbaaziz
      By nacerbaaziz
      hello guys
      how are you؟
      I hope to be fine.
      I have a question  please
      how do I get the menu item that was pressed without that contains a variable؟
      For example I have a menu  of Favorites and I want the script recognizes the existing path in the pressed item
      i'll repeat to tell the item does not contain a variable
      Is there any solution
      if you want to explain more I could write an example of what I want.
      Greetings to all,
      thanks in advance
    • 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
    • david1337
      By david1337
      Hey guys
      I hope that I can get a little help with this one
       
      In this GUI example using GUIListViewEx, I have a list based on items found in test.txt.
      _____________________________________________
      item1
      item2
      item3
      ____________________________________________
      etc....
       
      When an item is selected, and I click the GetInfo button, a message will show the text of that item.
      Is it possible to activate a case like that as soon as the item is selected, so I don't need a button to start the case?
       
      #include <GUIConstantsEx.au3> #include <GUIListViewEx.au3> Global $MainGUI_ManageItemList Global $File = "test.txt" Global $FileToArray = FileReadToArray("test.txt") Call ("MainGUI_ManageItemList") Func MainGUI_ManageItemList() Local $Button1 $MainGUI_ManageItemList = GUICreate("Manage Item List", 800, 400, -1, -1) $cLV = GUICtrlCreateListView("[items]", 10, 10, 400, 775, $LVS_NOCOLUMNHEADER) GUICtrlSetFont(-1, 12, 800, 0, "@Arial Unicode MS") _GUICtrlListView_SetColumnWidth($cLV, 0, 378) $Button1 = GUICtrlCreateButton("Button 1", 425, 10, 80, 30) $RemoveItem = GUICtrlCreateButton("Remove Item", 425, 50, 80, 30) $GetInfo = GUICtrlCreateButton("GetInfo", 425, 120, 80, 30) GUISetState(@SW_SHOW, $MainGUI_ManageItemList) ; Intialise ListView Global $iLV_Index = _GUIListViewEx_Init($cLV) ; Insert lines _GUIListViewEx_Insert($FileToArray, True) ; Register required messages _GUIListViewEx_MsgRegister(True, False, False, False) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop Case $Button1 MsgBox(0,"","Button 1 is pressed") Case $RemoveItem _GUIListViewEx_Delete() Case $GetInfo $ItemSelected = _GUICtrlListView_GetSelectedIndices($cLV, True) If IsArray($ItemSelected) And $ItemSelected[0] <> 0 Then ;This part makes sure it doesn't crash when no item is selected. $ItemSelectedText = _GUICtrlListView_GetItemText($cLV, $ItemSelected[1]) msgbox (0, "Selected item", $ItemSelectedText) EndIf EndSwitch WEnd EndFunc ;==>Main