Jump to content
Sign in to follow this  
Wingens

Read selected item in Treeview - FileManager

Recommended Posts

Wingens

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
LarsJ

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  

  • Similar Content

    • Bllee
      By Bllee
      every time i press "delete button" delete a line, but i can't add new line with info. the id line write normally but i can't change or add new info
      obs: looking at the "includes" one realizes that already tried of everything that knew
      my code
      #include <ButtonConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <GUIListBox.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #include <GUIConstantsEx.au3> #include <GuiImageList.au3> #include <GuiListView.au3> #include <GUIConstantsEx.au3> #include <GuiListView.au3> #include <MsgBoxConstants.au3> #include <WinAPI.au3> #include <StringConstants.au3> #include <Array.au3> #include <GuiListBox.au3> #Region ### START Koda GUI section ### Form= $Form1 = GUICreate("Form1", 612, 480, 192, 154) $MenuItem1 = GUICtrlCreateMenu("&Arquivo") $MenuItem2 = GUICtrlCreateMenuItem("Salvar Ctrl+S", $MenuItem1, $SS_CENTER) $MenuItem4 = GUICtrlCreateMenuItem("Carregar Ctrl+O", $MenuItem1, $SS_CENTER) $MenuItem3 = GUICtrlCreateMenuItem("Salvar Como Shitft+Ctrl+S", $MenuItem1, $SS_CENTER) $Group1 = GUICtrlCreateGroup("", 21, 8, 569, 97, $SS_CENTER) $Input1 = GUICtrlCreateInput("ID COR", 122, 32, 97, 21, $SS_CENTER) $Input2 = GUICtrlCreateInput("REGIÃO", 138, 64, 249, 21, $SS_CENTER) $Input3 = GUICtrlCreateInput("Label", 264, 30, 121, 21, $SS_CENTER) $Label1 = GUICtrlCreateLabel("Label", 232, 32, 30, 17, $SS_CENTER) $Button1 = GUICtrlCreateButton("Capturar Cor", 42, 30, 75, 25, $SS_CENTER) $Button2 = GUICtrlCreateButton("Capturar Região", 42, 62, 91, 25, $SS_CENTER) $Button3 = GUICtrlCreateButton("ADD", 442, 24, 113, 33) $Button5 = GUICtrlCreateButton("Delet", 442, 64, 113, 33) $Group2 = GUICtrlCreateGroup("Editar ID selecionado", 8, 384, 601, 57, $SS_CENTER) $Input7 = GUICtrlCreateInput("ID", 10, 402, 40,21, $SS_CENTER) $Input4 = GUICtrlCreateInput("COR", 51, 402, 121, 21, $SS_CENTER) $Input5 = GUICtrlCreateInput("REGIÃO", 173, 402, 249, 21, $SS_CENTER) $Input6 = GUICtrlCreateInput("LABEL", 423, 402, 121, 21, $SS_CENTER) $Button4 = GUICtrlCreateButton("\/", 544, 400, 30, 25, $SS_CENTER) $Button6 = GUICtrlCreateButton("/\", 574, 400, 30, 25, $SS_CENTER) ; Add column Global $id = Int(0) $List1 = GUICtrlCreateListView("",5, 120, 601, 253, $SS_CENTER) _GUICtrlListView_AddColumn($List1, "ID",40) _GUICtrlListView_AddColumn($List1, "COR",100) _GUICtrlListView_AddColumn($List1, "COORDENADA",305) _GUICtrlListView_AddColumn($List1, "LABEL",150) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $MenuItem2 MsgBox($MB_SYSTEMMODAL, "ctrl + s", "Nothing Here",1) Case $MenuItem3 MsgBox($MB_SYSTEMMODAL, "shift+ctrl s", "Nothing Here",1) Case $MenuItem4 MsgBox($MB_SYSTEMMODAL, "ctrl + o", "Nothing Here",1) Case $Button1 MsgBox($MB_SYSTEMMODAL, "Capturar cor", "Aperte espaço para capturar a cor") GUICtrlSetData($Input1,MouseColor()) Case $Button2 MsgBox($MB_SYSTEMMODAL, "capturar região", "Aperte espaço para capturar a posição inicial") local $mouse = MouseGetPos() $x_init = $mouse[0] $y_init = $mouse[1] MsgBox($MB_SYSTEMMODAL, "capturar região", "Aperte espaço para capturar a posição final") local $mouse = MouseGetPos() $x_end = $mouse[0] $y_end = $mouse[1] GUICtrlSetData($Input2,$x_init&','&$y_init&' ; '&$x_end&','&$y_end) Case $Button3 _GUICtrlListView_AddItem($List1, $id, 1) ; id _GUICtrlListView_AddSubItem($List1, $id, GUICtrlRead($Input1), 1, 1) ;cor _GUICtrlListView_AddSubItem($List1, $id, GUICtrlRead($Input2), 2, 1) ;coordenada _GUICtrlListView_AddSubItem($List1, $id, GUICtrlRead($Input3), 3, 1) ;label $id= $id + 1 Case $Button4 $Item = _GUICtrlListView_GetSelectedIndices($List1) $Sid = _GUICtrlListView_GetItem($List1, Number($Item),0) $Scor = _GUICtrlListView_GetItem($List1, Number($Item),1) $Scord = _GUICtrlListView_GetItem($List1, Number($Item),2) $Slabel = _GUICtrlListView_GetItem($List1, Number($Item),3) ; get info GUICtrlSetData($Input7,$Sid[3]) GUICtrlSetData($Input4,$Scor[3]) GUICtrlSetData($Input5,$Scord[3]) GUICtrlSetData($Input6,$Slabel[3]) Case $Button5 _GUICtrlListView_DeleteItemsSelected($List1) Case $Button6 ; give back inf if GUICtrlRead($Input7) <> "ID" Then _GUICtrlListView_SetItem($List1, GUICtrlRead($Input4), GUICtrlRead($Input7),1) ;cor _GUICtrlListView_SetItem($List1, GUICtrlRead($Input5), GUICtrlRead($Input7),2) ;coordenada _GUICtrlListView_SetItem($List1, GUICtrlRead($Input6), GUICtrlRead($Input7),3) ;coordenada EndIf EndSwitch WEnd Func MouseColor() local $mouse = MouseGetPos() Return Hex(PixelGetColor($mouse[0],$mouse[1]), 6) EndFunc  
    • FrancescoDiMuro
      By FrancescoDiMuro
      Good evening everyone
      I am building a management for the company I work with, and I just imported a real amount of rows ( about 29000 ), in my SQLite DB.
      The thing I am not understanding, is the time that the script takes to build this amount of rows in the ListView.
      I didn't measure it, but I think it took 2 minutes or so to create each ListView item...
      It is normal that it takes so much time?
      What can I do to improve the creation of the items?

      Here's the code I am using to query and to create ListView items...
      ; Articles ListView: Global $lvwArticles = GUICtrlCreateListView("ID|Fornitore|Codice|Descrizione|EU|Prezzo|Sconto Applicato|Note", 14, 87, 1507, 660, BitOR($GUI_SS_DEFAULT_LISTVIEW,$LVS_SORTASCENDING,$LVS_SORTDESCENDING), BitOR($WS_EX_CLIENTEDGE,$LVS_EX_GRIDLINES,$LVS_EX_FULLROWSELECT)) ; Query $strQuery = "SELECT * FROM ARTICOLI;" ; Query Execution _SQLite_GetTable2d($objDatabase, $strQuery, $arrResult, $intRows, $intColumns) If @error Then ; Error Handling Else ; Cleaning the ListView _GUICtrlListView_DeleteAllItems($lvwArticles) If @error Then ; Error Handling Else ; No records in the Table If UBound($arrResult) < 2 Then ; Error Handling Else _GUICtrlListView_BeginUpdate($lvwArticles) For $intCounter = 1 To UBound($arrResult) - 1 $strListViewItem = $arrResult[$intCounter][0] & "|" & _ $arrResult[$intCounter][1] & "|" & _ $arrResult[$intCounter][2] & "|" & _ $arrResult[$intCounter][3] & "|" & _ $arrResult[$intCounter][4] & "|" & _ $arrResult[$intCounter][5] & "|" & _ $arrResult[$intCounter][6] & "|" & _ $arrResult[$intCounter][7] $objListViewItem = GUICtrlCreateListViewItem($strListViewItem, $lvwArticles) Next _GUICtrlListView_EndUpdate($lvwArticles) EndIf EndIf EndIf Thanks in advance


      Best Regards.
    • kelso
      By kelso
      Hello Guru's,
       
      I'm trying to write an autoit script to select from the dropdown list as you see in the attached picture. 
      I read the help page for _IEFormElementOptionSelect, but I cannot grasp how to correlate that with the source code that I'm seeing. any suggestions?

    • 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
    • nacerbaaziz
      By nacerbaaziz
      Hello
      I have an inquiry please
      when i use the control get focus
      it don't detect the menu item
      there are any Method to do that?
      i'll use that to make a custom screen reader in my program
      please help me to do that
      I am waiting for your answers
      greetings to you
      Thank you in advance
       
×