Jump to content

Recommended Posts

Hi people, I'm trying to dynamically populate a TreeView based on a script from Water that I modified for my needs. but I can't figured it out how to make it work.

Here what I have in case anyone can help me, thanks in advance:

#include <array.au3>
#include <GUIConstantsEx.au3>
#include <GuiTreeView.au3>

Local $aTV1[14][3] = [ _
        ['1', 'Text 1', -1], _ ;-1 idicates it's an index item
        ['2', 'Text 2',-1], _
        ['3', 'Text 3',-1], _
        ['1-1', 'Text 1-1',0], _
        ['1-2', 'Text 1-2',0], _
        ['2-1', 'Text 2-1',0], _
        ['2-2', 'Text 2-2',0], _
        ['2-1-1', 'Text 2-1-1',0], _
        ['2-1-2', 'Text 2-1-2',0], _
        ['2-1-2-1', 'Text 2-1-2-1',0], _
        ['2-1-2-1-1', 'Text 2-1-2-1-1',0], _
        ['3-1', 'Text 3-1',0], _
        ['3-1-1', 'Text 3-1-1',0], _
        ['3-1-1-1', 'Text 3-1-1-1',0]]
;_ArrayDisplay($aTV1)

Local $Gui = GUICreate('TreeView Example', 500, 600)
Local $tv = GUICtrlCreateTreeView(10, 30, 450, 550)

GUISetState()
_pop_treeview($tv, $aTV1)

While 1
    Switch GUIGetMsg()
        Case $gui_event_close
            Exit
    EndSwitch
WEnd

Func _pop_treeview($hTV, $array)
    Local $TimeInitial = TimerInit(), $Hours, $Mins, $Secs, $item
    _ArraySort($array, 0, 0, 0, 0) ;------------------------- sort Ascending on column 0
    ;_ArrayDisplay($array)

    $idxroot = _GUICtrlTreeView_Add($hTV, 0, "index")
    For $i = 0 To UBound($array) - 1
        ConsoleWrite("$array[" & $i & "][" & 0 & "] = " & $array[$i][0] & @CRLF)

        If $array[$i][0] = '' Then ExitLoop ;--------------- Exit at first empty element
        $item = ''
        If $array[$i][2] = -1 Then ;------------------------ Add root element
            _GUICtrlTreeView_AddChild($hTV, $idxroot, $array[$i][1])
        Else
            $item = StringLeft($array[$i][0], StringInStr($array[$i][0], "-", 1, -1) - 1)
            ConsoleWrite("$item = " & $item & @CRLF)
            $Found = _ArrayBinarySearch($array, $item, 0, 0, 0) ;search on column 0
            Switch $Found
                Case 0 ;----------------------------------- Value wasn't found in array
                    ConsoleWrite("Item NOT found @error= " & @error & @CRLF)
                Case Else
                    ConsoleWrite("Item found " & @CRLF)
                    _GUICtrlTreeView_AddChild($hTV, $Found, $array[$i][1])
            EndSwitch
        EndIf
    Next
    _GUICtrlTreeView_Expand($hTV)
EndFunc   ;==>_pop_treeview

 

Link to post
Share on other sites

Recursion is the key of simplicity.

#include <array.au3>
#include <GUIConstantsEx.au3>
#include <GuiTreeView.au3>

Local $aTV1[14][3] = [ _
        ['1', 'Text 1', -1], _ ;-1 idicates it's an index item
        ['2', 'Text 2',-1], _
        ['3', 'Text 3',-1], _
        ['1-1', 'Text 1-1',0], _
        ['1-2', 'Text 1-2',0], _
        ['2-1', 'Text 2-1',0], _
        ['2-2', 'Text 2-2',0], _
        ['2-1-1', 'Text 2-1-1',0], _
        ['2-1-2', 'Text 2-1-2',0], _
        ['2-1-2-1', 'Text 2-1-2-1',0], _
        ['2-1-2-1-1', 'Text 2-1-2-1-1',0], _
        ['3-1', 'Text 3-1',0], _
        ['3-1-1', 'Text 3-1-1',0], _
        ['3-1-1-1', 'Text 3-1-1-1',0]]

Local $Gui = GUICreate('TreeView Example', 500, 600)
Local $tv = GUICtrlCreateTreeView(10, 30, 450, 550)
_ArraySort($aTV1, 0, 0, 0, 0) ;------------------------- sort Ascending on column 0
;_ArrayDisplay($aTV1)
Local $idxroot = _GUICtrlTreeView_Add($tv, 0, "index")

GUISetState()

Global $iIdx = 0
Recurse($tv, $aTV1, $idxroot, "")
_GUICtrlTreeView_Expand($tv)

While 1
    Switch GUIGetMsg()
        Case $gui_event_close
            Exit
    EndSwitch
WEnd

Func Recurse (ByRef $hTV, ByRef $array, $hParent, $sTxt)
  While $iIdx < UBound($array) And $sTxt = StringTrimRight($array[$iIdx][0],2)
    $hNew = _GUICtrlTreeView_AddChild($hTV, $hParent, $array[$iIdx][1])
    $iIdx += 1
    Recurse ($htv, $array, $hNew, $array[$iIdx-1][0])
  WEnd
EndFunc

 

Edited by Nine
Link to post
Share on other sites

Hi Nine,

what a clever idea to cycle on itself for recursion. Your example works fine.

I'm now trying to populate the TreeView from a file but without luck.

The file may contain any number of branches and sub-branches which I don't know before hand,  and could be over 100, so I don't know how to change StringTrimRight($array[$iIdx][0],3) to accept this variable number.

This is what I have so far and I attach the testing file if you can help me please.

#include <array.au3>
#include <File.au3>
#include <GUIConstantsEx.au3>
#include <GuiTreeView.au3>

Local $aTV1
$File = @ScriptDir & "\" & "File.txt"
_FileReadToArray($File, $aTV1, 0, "|")

Local $Gui = GUICreate('TreeView Example', 500, 600)
Local $tv = GUICtrlCreateTreeView(10, 30, 450, 550)
;_ArraySort($aTV1, 0, 0, 0, 0) ;------------------------- sort Ascending on column 0
;_ArrayDisplay($aTV1)
Local $idxroot = _GUICtrlTreeView_Add($tv, 0, "index")

GUISetState()

Global $iIdx = 0
Recurse($tv, $aTV1, $idxroot, "")
_GUICtrlTreeView_Expand($tv)

While 1
    Switch GUIGetMsg()
        Case $gui_event_close
            Exit
    EndSwitch
WEnd

Func Recurse (ByRef $hTV, ByRef $array, $hParent, $sTxt)
  While $iIdx < UBound($array) And $sTxt = StringTrimRight($array[$iIdx][0],3)
    $hNew = _GUICtrlTreeView_AddChild($hTV, $hParent, $array[$iIdx][1])
    $iIdx += 1
    Recurse ($htv, $array, $hNew, $array[$iIdx-1][0])
  WEnd
EndFunc

 

File.txt

Link to post
Share on other sites

With that file, you will need to reformat the primary key to use that Recurse function with success.  Your OP example uses only 1 numeric char per level. In the text file, you are using 1 to 3 numeric chars per level.  So before calling the Recurse function, make sure that all levels show the same number of char (in that case 3).

For example 2-1-1 should become 002-001-001.  Then your can properly sort the array and run the Recurse function with now a right trim of 4.  Try to create the reformat function and if you have problem come back here with your attempt.

Link to post
Share on other sites

Nine, I modified the script as per your recommendations  and it works perfect now, so I attached it in case anyone may need it. 

I really appreciate your help.

#include <array.au3>
#include <File.au3>
#include <GUIConstantsEx.au3>
#include <GuiTreeView.au3>

Local $aTV1
;el archivo no puede tener lineas en blanco porque sino no funciona
$File = @ScriptDir & "\" & "File.txt"
_FileReadToArray($File, $aTV1, 0, "|")

Local $Gui = GUICreate('TreeView Example', 500, 600)
Local $tv = GUICtrlCreateTreeView(10, 30, 450, 550)
_ArraySort($aTV1, 0, 0, 0, 0) ;------------------------- sort Ascending on column 0
;_ArrayDisplay($aTV1)
Local $idxroot = _GUICtrlTreeView_Add($tv, 0, "index")

GUISetState()

Global $iIdx = 0
Recurse($tv, $aTV1, $idxroot, "")
_GUICtrlTreeView_Expand($tv)

While 1
    Switch GUIGetMsg()
        Case $gui_event_close
            Exit
    EndSwitch
WEnd

Func Recurse (ByRef $hTV, ByRef $array, $hParent, $sTxt)
  While $iIdx < UBound($array) And $sTxt = StringTrimRight($array[$iIdx][0],4)
    $hNew = _GUICtrlTreeView_AddChild($hTV, $hParent, $array[$iIdx][1])
    $iIdx += 1
    Recurse ($htv, $array, $hNew, $array[$iIdx-1][0])
  WEnd
EndFunc

 

File.txt

Link to post
Share on other sites

Glad it is working !  But I first thought you would create a function within the script to convert the primary key on the fly.  It is all good if you can modify the input file beforehand instead.

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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By benners
      I have a GUI that contains a treeview and what I am trying to do is to get the context menu to only show when an item is right clicked. Currently, as it's assigned to the treeview, the context menu activates whenever the treeview is clicked. Rather than keep enabling\disabling the menu items I am in need of a way limit the menus popup.
      One of the options on the menu will open another GUI and disable the parent, for this reason, I cannot do much in the WM_NOTIFY portion as it hangs the program and I don't want to hang around blocking the messages.
      The other option is to create a menu for each treeview item when the are added to the list. This will fix the issue but there maybe 100+ items and I don't want to have that many menus all of which have the same 2/3 options.
      I am currently looking at using an Adlib function as mentioned here by Melba. I could create and destroy the menu each time and unregister the adlib when the function closes.
      Anyway, a producer code is below if there are any other ideas, or I've missed the obvious. Cheers
      #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <TreeViewConstants.au3> #include <GuiTreeView.au3> #include <ButtonConstants.au3> #include <GUIListBox.au3> Opt('MustDeclareVars', 1) _Main() Func _Main() Local $hGUI = GUICreate("TreeView", 400, 300) Global $cTreeView = GUICtrlCreateTreeView(10, 10, 380, 280) Local $cParent = GUICtrlCreateTreeViewItem('List', $cTreeView) For $i = 1 To 5 GUICtrlCreateTreeViewItem('Item ' & $i, $cParent) Next Local $cCMenu = GUICtrlCreateContextMenu($cTreeView) Global $cCMenuEdit = GUICtrlCreateMenuItem("Edit", $cCMenu) GUICtrlSetState(-1, $GUI_DISABLE) _GUICtrlTreeView_Expand($cTreeView) GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY") GUISetState(@SW_SHOW) Local $nMsg While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $cCMenuEdit EndSwitch WEnd EndFunc ;==>_Main Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam) #forceref $hWnd, $iMsg, $wParam Local $tNMHDR = DllStructCreate($tagNMHDR, $lParam) Local $hWndFrom = DllStructGetData($tNMHDR, "hWndFrom") Local $iIDFrom = DllStructGetData($tNMHDR, "IDFrom") Local $iCode = DllStructGetData($tNMHDR, "Code") Switch $iIDFrom Case $cTreeView Switch $iCode Case $NM_RCLICK Local $tPoint = _WinAPI_GetMousePos(True, $hWndFrom) Local $tHitTest = _GUICtrlTreeView_HitTestEx($hWndFrom, DllStructGetData($tPoint, 1), DllStructGetData($tPoint, 2)) If BitAND(DllStructGetData($tHitTest, "Flags"), $TVHT_ONITEM) Then If _GUICtrlTreeView_Level($cTreeView, DllStructGetData($tHitTest, 'Item')) > 0 Then GUICtrlSetState($cCMenuEdit, $GUI_ENABLE) Else GUICtrlSetState($cCMenuEdit, $GUI_DISABLE) EndIf _GUICtrlTreeView_SelectItem($hWndFrom, DllStructGetData($tHitTest, 'Item')) Else GUICtrlSetState($cCMenuEdit, $GUI_DISABLE) EndIf EndSwitch EndSwitch Return $GUI_RUNDEFMSG EndFunc ;==>WM_NOTIFY  
    • By Vipan
      Hi,
      I have developed Java maven based framework and integrated AutoIt in it. I am automated a desktop application. During automation, I am facing below issue. Can someone please guide me?
      1. I am unable to select a menu option from a nested tree view:
      Example-
      TreeTitle1
         TreeTitle1.1
              TreeOption ToSelect
      In above case, TreeTitle1 is a folder in which TreeTitle2 is another folder and the TreeOptionToSelect is the one i want to select.
      I tried below command:
      AutoItX autoIt = new AutoItX();
      autoIt.controlTreeViewExpand(pagetitle,"",treecontrolname,"#0|#0");
      autoIt.controlTreeViewSelect(pagetitle,"",treecontrolname,"#0|#0|#0");
       
      But it didn't work... kindly help
    • By MesterPerfect
      good morning
      this is the first post here in the autoit forums
      i hope that you can help me in my problem
      i have a JSON encoded
      it a map of my forums
      where i want to make a treeview that have the same type of map
      e.g
      a system (as category)
      windows (as sub category)
      software (as an child item in the windows category)
      .....
      i don't know how to do that
      so, i know that i can do that using the json functions
      but i need your help about how we can do it as the type that i told you
      by the way i need to put the sub info for each item in an array that give me the ability to manage my items
      e.g
      can post thread
      can reply
      message cound ...
      you just give me a small example and i can continue.
      am sorry if this against the rules of the forum.
      but i realy searched a lot but i couldn't
      i hope some one give me the way.
      thank you very much in advance
       
      here is the link of json forum
      https://www.autoitscript.com/forum/topic/148114-a-non-strict-json-udf-jsmn/
      and here is my encoded json file
       
      { "tree_map": { "0": [ 1, 5, 6, 7 ], "1": [ 2 ], "2": [ 4 ], "5": [ 3 ], "6": [ 8 ], "8": [ 9, 10 ] }, "nodes": [ { "breadcrumbs": [], "description": "", "display_in_list": true, "display_order": 1, "node_id": 1, "node_name": null, "node_type_id": "Category", "parent_node_id": 0, "title": "Main category", "type_data": {} }, { "breadcrumbs": [ { "node_id": 1, "title": "Main category", "node_type_id": "Category" } ], "description": "", "display_in_list": true, "display_order": 1, "node_id": 2, "node_name": null, "node_type_id": "Forum", "parent_node_id": 1, "title": "Main forum", "type_data": { "allow_poll": true, "allow_posting": true, "can_create_thread": true, "can_upload_attachment": true, "discussion_count": 0, "last_post_date": 0, "last_post_id": 0, "last_post_username": "", "last_thread_id": 0, "last_thread_prefix_id": 0, "last_thread_title": "", "message_count": 0, "min_tags": 0, "require_prefix": false } }, { "breadcrumbs": [ { "node_id": 1, "title": "Main category", "node_type_id": "Category" }, { "node_id": 2, "title": "Main forum", "node_type_id": "Forum" } ], "description": "", "display_in_list": true, "display_order": 1, "node_id": 4, "node_name": null, "node_type_id": "Forum", "parent_node_id": 2, "title": "my forums1", "type_data": { "allow_poll": true, "allow_posting": true, "can_create_thread": true, "can_upload_attachment": true, "discussion_count": 0, "last_post_date": 0, "last_post_id": 0, "last_post_username": "", "last_thread_id": 0, "last_thread_prefix_id": 0, "last_thread_title": "", "message_count": 0, "min_tags": 0, "require_prefix": false } }, { "breadcrumbs": [], "description": "", "display_in_list": true, "display_order": 2, "node_id": 5, "node_name": null, "node_type_id": "Category", "parent_node_id": 0, "title": "Perfect", "type_data": {} }, { "breadcrumbs": [ { "node_id": 5, "title": "Perfect", "node_type_id": "Category" } ], "description": "", "display_in_list": true, "display_order": 2, "node_id": 3, "node_name": null, "node_type_id": "Forum", "parent_node_id": 5, "title": "ahmed", "type_data": { "allow_poll": true, "allow_posting": true, "can_create_thread": true, "can_upload_attachment": true, "discussion_count": 0, "last_post_date": 0, "last_post_id": 0, "last_post_username": "", "last_thread_id": 0, "last_thread_prefix_id": 0, "last_thread_title": "", "message_count": 0, "min_tags": 0, "require_prefix": false } }, { "breadcrumbs": [], "description": "", "display_in_list": true, "display_order": 3, "node_id": 6, "node_name": null, "node_type_id": "Forum", "parent_node_id": 0, "title": "autoit", "type_data": { "allow_poll": true, "allow_posting": true, "can_create_thread": true, "can_upload_attachment": true, "discussion_count": 0, "last_post_date": 0, "last_post_id": 0, "last_post_username": "", "last_thread_id": 0, "last_thread_prefix_id": 0, "last_thread_title": "", "message_count": 0, "min_tags": 0, "require_prefix": false } }, { "breadcrumbs": [ { "node_id": 6, "title": "autoit", "node_type_id": "Forum" } ], "description": "", "display_in_list": true, "display_order": 3, "node_id": 8, "node_name": null, "node_type_id": "Forum", "parent_node_id": 6, "title": "examples", "type_data": { "allow_poll": true, "allow_posting": true, "can_create_thread": true, "can_upload_attachment": true, "discussion_count": 0, "last_post_date": 0, "last_post_id": 0, "last_post_username": "", "last_thread_id": 0, "last_thread_prefix_id": 0, "last_thread_title": "", "message_count": 0, "min_tags": 0, "require_prefix": false } }, { "breadcrumbs": [ { "node_id": 6, "title": "autoit", "node_type_id": "Forum" }, { "node_id": 8, "title": "examples", "node_type_id": "Forum" } ], "description": "", "display_in_list": true, "display_order": 3, "node_id": 9, "node_name": null, "node_type_id": "Forum", "parent_node_id": 8, "title": "GUI", "type_data": { "allow_poll": true, "allow_posting": true, "can_create_thread": true, "can_upload_attachment": true, "discussion_count": 0, "last_post_date": 0, "last_post_id": 0, "last_post_username": "", "last_thread_id": 0, "last_thread_prefix_id": 0, "last_thread_title": "", "message_count": 0, "min_tags": 0, "require_prefix": false } }, { "breadcrumbs": [ { "node_id": 6, "title": "autoit", "node_type_id": "Forum" }, { "node_id": 8, "title": "examples", "node_type_id": "Forum" } ], "description": "", "display_in_list": true, "display_order": 31, "node_id": 10, "node_name": null, "node_type_id": "Forum", "parent_node_id": 8, "title": "windowEX", "type_data": { "allow_poll": true, "allow_posting": true, "can_create_thread": true, "can_upload_attachment": true, "discussion_count": 0, "last_post_date": 0, "last_post_id": 0, "last_post_username": "", "last_thread_id": 0, "last_thread_prefix_id": 0, "last_thread_title": "", "message_count": 0, "min_tags": 0, "require_prefix": false } }, { "breadcrumbs": [], "description": "", "display_in_list": true, "display_order": 4, "node_id": 7, "node_name": null, "node_type_id": "Category", "parent_node_id": 0, "title": "vbs", "type_data": {} } ] }  
    • By Burgs
      Hello,
        I am having great difficulty with something I would have thought to be fairly easy.  I have an array containing (military) organization strings, similar to this:
      42nd Battalion
       Company A
        1st Platoon
         1st Squad
         2nd Squad
         3rd Squad
        2nd Platoon
         ...1st, 2nd, 3rd Squad
        3rd Platoon
        ...1st, 2nd, 3rd Squad
       Company B
        ...etc...
      I also have an associated array that contains the 'hierarchy' information associated with each string..."0", "1","2", etc   ...therefore using example above the "0" would associate with "42nd Battalion", while "1" would be "Company A", "2" is "1st Platoon", etc.  Each sequential number is a 'child' of the preceding number.  Therefore the information above would appear in the array as "0", "1", "2", "3", "3", "3", "2", "3", "3", "3", "2", "3", "3", "3", "1", etc...
        My issue is that I'm having great difficulty creating a 'Treeview' control using this information.  Mainly due to the fact that entries can, and will...repeat (for example multiple entries for "Company A", "Company B", "1st Platoon", "1st Squad", etc, etc).  The following code is operative, however it does not create the treeview items as 'child' entries of one another...it simply lists each item as a separate entity.
      For $_populate = 0 to Ubound($_HIERARCHY) - 1 _GUICtrlTreeView_BeginUpdate($idTreeView) _GUICtrlTreeView_Add($idTreeView, $_HIERARCHY[$_populate], String($_STRUCTURES[$_populate])) _GUICtrlTreeView_EndUpdate($idTreeView) Next ;Next $_populate, cycle thru '$_HIERARCHY' array to build 'TREEVIEW' Controls As I mentioned earlier I thought I could manage this...however i'm pulling out quite a bit of my hair trying to get this working properly...any help would be appreciated.  I thank you in advance. 
       
    • By xtcislove
      Hello,
      i stuck again,
      Im using this function to create a treeview from root dir.
       
      ;https://autoit.de/index.php?thread/86082-treeview-root-verbergen/&postID=691139#post691139 #include <File.au3> #include <WindowsConstants.au3> Global $sPath = @ScriptDir Global $hGui = GUICreate('TreeView-Example', 400, 600) Global $idTreeView = GUICtrlCreateTreeView(10, 10, 380, 580, Default, $WS_EX_CLIENTEDGE) GUISetState() _CreatePath($sPath, $idTreeView) Do Until GUIGetMsg() = -3 Func _CreatePath($sPath, $idParent) Local $aFolder, $aFiles, $idItem If StringRight($sPath, 1) <> '\' Then $sPath &= '\' $aFolder = _FileListToArray($sPath, '*', $FLTA_FOLDERS) If Not @error Then For $i = 1 To $aFolder[0] $idItem = GUICtrlCreateTreeViewItem($aFolder[$i], $idParent) _CreatePath($sPath & $aFolder[$i], $idItem) Next EndIf $aFiles = _FileListToArray($sPath, '*', $FLTA_FILES) If @error Then Return For $i = 1 To $aFiles[0] $idItem = GUICtrlCreateTreeViewItem($aFiles[$i], $idParent) Next EndFunc Folder Structure:

      Folder1
      Folder2
      Folder3
      If a file exists in multiple folders, i like to color it red, if not green. 

      I know how to do this for files, but nut for the folders.

      Because if there is only 1 file in Folder2 that is also in Folder1 that it should only color this single file red, inlcuding its whole tree.

      The Folder1 and Folder2 should be red in this case, too. Other files and trees should stay green.
       
      Edit:
      Basically i like to color a file and its belonging tree red if the file exists more than 1 time.
×
×
  • Create New...