Jump to content
Sign in to follow this  
Burgs

Treeview creation frustrations

Recommended Posts

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. 
 

Edited by Burgs

Share this post


Link to post
Share on other sites

You need a little more code:

#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7

Opt( "MustDeclareVars", 1 )

#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <GuiTreeView.au3>

Example()

Func Example()
  Local $aArray = [ _
    [ 0, "42nd Battalion" ], _
    [ 1, "Company A"      ], _
    [ 2, "1st Platoon"    ], _
    [ 3, "1st Squad"      ], _
    [ 3, "2nd Squad"      ], _
    [ 3, "3rd Squad"      ], _
    [ 2, "2nd Platoon"    ], _
    [ 3, "1st Squad"      ], _
    [ 3, "2nd Squad"      ], _
    [ 3, "3rd Squad"      ], _
    [ 2, "3rd Platoon"    ], _
    [ 3, "1st Squad"      ], _
    [ 3, "2nd Squad"      ], _
    [ 3, "3rd Squad"      ], _
    [ 1, "Company B"      ] ]

  ; Create GUI
  Local $hGui = GUICreate( "TreeView from array", 400, 300 )

  ; Create TreeView
  Local $idTV = GUICtrlCreateTreeView( 4, 4, 392, 292, $GUI_SS_DEFAULT_TREEVIEW, $WS_EX_CLIENTEDGE )

  ; --- Fill TreeView ---

  Local $iItems = UBound( $aArray ), $aLevels[$iItems], $iLevel, $iLevelPrev = 0, $hItem

  ; Add root
  $hItem = _GUICtrlTreeView_Add( $idTV, 0, $aArray[0][1] )
  $aLevels[0] = $hItem                     ; $aLevels[$iLevel] contains the last item of that level

  For $i = 1 To $iItems - 1
    $iLevel = $aArray[$i][0]
    If $iLevel <> $iLevelPrev Then
      If $iLevel > $iLevelPrev Then        ; A child of the previous level
        $hItem = _GUICtrlTreeView_AddChild( $idTV, $aLevels[$iLevelPrev], $aArray[$i][1] )
      Else ; $iLevel < $iLevelPrev         ; A sibling of the level
        $hItem = _GUICtrlTreeView_Add( $idTV, $aLevels[$iLevel], $aArray[$i][1] )
      EndIf
      $aLevels[$iLevel] = $hItem           ; $aLevels[$iLevel] contains the last item of that level
      $iLevelPrev = $iLevel
    Else ; $iLevel = $iLevelPrev           ; A sibling of the level
      $hItem = _GUICtrlTreeView_Add( $idTV, $aLevels[$iLevel], $aArray[$i][1] )
      $aLevels[$iLevel] = $hItem           ; $aLevels[$iLevel] contains the last item of that level
    EndIf
  Next

  _GUICtrlTreeView_Expand( $idTV )

  ; ---------------------

  ; Show GUI
  GUISetState( @SW_SHOW, $hGui )

  ; Message loop
  While 1
    Switch GUIGetMsg()
      Case $GUI_EVENT_CLOSE
        ExitLoop
    EndSwitch
  WEnd

  GUIDelete( $hGui )
EndFunc

 

Share this post


Link to post
Share on other sites

Greetings,

Sorry for the late reply.

Thank you for the response...I tested your code and it seems to work fine.  In the meantime I also arrived at another solution which seems to work, although not yet heavily tested:

ReDim $_TREE_BRANCH[Ubound($_HIERARCHY)]

 GUISwitch($hGUI)

  For $_populate = 0 to Ubound($_HIERARCHY) - 1


  _GUICtrlTreeView_BeginUpdate($idTreeView)
   if Int($_HIERARCHY[$_populate]) == 0 Then
   $_TREE_BRANCH[$_populate] = _GUICtrlTreeView_AddChild($idTreeView, $hItem, String($_STRUCTURES[$_populate]))
   Else
    if Int($_HIERARCHY[$_populate]) == Int($_HIERARCHY[$_populate - 1] + 1) Then
    $_TREE_BRANCH[$_populate] = _GUICtrlTreeView_AddChild($idTreeView, $_TREE_BRANCH[$_populate - 1], String($_STRUCTURES[$_populate]))
   EndIf  ;Int($_HIERARCHY[$_populate]) EQUALS the 'previous' entry "+ 1"...
          ;therefore MUST be a 'subordinate' to that 'previous' entry...
   if Int($_HIERARCHY[$_populate]) == Int($_HIERARCHY[$_populate - 1]) Then
   $_parent = _GUICtrlTreeView_GetParentHandle($idTreeView, $_TREE_BRANCH[$_populate - 1])
    if $_parent <> 0 Then $_TREE_BRANCH[$_populate] = _GUICtrlTreeView_AddChild($idTreeView, $_parent, String($_STRUCTURES[$_populate]))
   EndIf  ;'$_HIERARCHY[$_populate]' EQUALS 'previous' entry...
          ;use same 'current superior' and/or 'parent'...
   if (Int($_HIERARCHY[$_populate]) <> Int($_HIERARCHY[$_populate - 1] + 1)) AND (Int($_HIERARCHY[$_populate]) <> Int($_HIERARCHY[$_populate - 1])) Then
    if (Int($_HIERARCHY[$_populate]) < Int($_HIERARCHY[$_populate - 1])) AND (Int($_HIERARCHY[$_populate]) <> 0) Then $_LOOKUP = _ArraySearch($_HIERARCHY, Int($_HIERARCHY[$_populate] - 1), 0, $_populate - 1, 0, 0, 0)
      ;search '$_HIERARCHY' array from END (from '$_populate' - 1) to BEGINNING to locate the 'superior'
      ;to the current '$_populate' value...!
     if Int($_LOOKUP) <> -1 Then $_TREE_BRANCH[$_populate] = _GUICtrlTreeView_AddChild($idTreeView, $_TREE_BRANCH[$_LOOKUP], String($_STRUCTURES[$_populate]))
    EndIf  ;'$_populate' is less than '$_populate - 1' AND is NOT "0"...
   EndIf  ;'$_HIERARCHY[$_populate]' does NOT EQUAL 'previous' entry "+ 1" OR 'previous' entry...MUST be a 'non-sequential' UNIT...

   EndIf  ;'$_populate' is "0"...OR NOT...

  _GUICtrlTreeView_EndUpdate($idTreeView)

  Next  ;Next $_populate, cycle thru '$_HIERARCHY' array to build 'TREEVIEW' Controls

I suppose either way should get the job accomplished.  I thank you again very much !

Edited by Burgs

Share this post


Link to post
Share on other sites

haha yes I know...I always do that out of habit...but I've never had any problems with it...thanks for posting

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By jcpetu
      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  
    • 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 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.
    • By xtcislove
      Hello,

      im searching like 6 hours and i didnt found the right solution.

      Im trying to build a treeview from a directory and its subfolders etc.

      This function give me right treeview, but i cant color each item seperated.

       
      #include <GuiTreeView.au3> $hGui = GUICreate("Demo1", 600, 400) $hTreeView = _GUICtrlTreeView_Create($hGui, 10, 10, 580, 380) GUISetState() _GUICtrlTreeView_BeginUpdate($hTreeView) ListFiles_ToTreeView(@ScriptDir, 0) _GUICtrlTreeView_EndUpdate($hTreeView) Do Until GUIGetMsg() = -3 Func ListFiles_ToTreeView($sSourceFolder, $hItem) Local $sFile ; Force a trailing \ If StringRight($sSourceFolder, 1) <> "\" Then $sSourceFolder &= "\" ; Start the search Local $hSearch = FileFindFirstFile($sSourceFolder & "*.*") ; If no files found then return If $hSearch = -1 Then Return ; This is where we break the recursive loop <<<<<<<<<<<<<<<<<<<<<<<<<< ; Now run through the contents of the folder While 1 ; Get next match $sFile = FileFindNextFile($hSearch) ; If no more files then close search handle and return If @error Then ExitLoop ; This is where we break the recursive loop <<<<<<<<<<<<<<<<<<<<<<<<<< ; Check if a folder If @extended Then ; If so then call the function recursively ListFiles_ToTreeView($sSourceFolder & $sFile, _GUICtrlTreeView_AddChild($hTreeView, $hItem, $sFile)) Else ; If a file than write path and name _GUICtrlTreeView_AddChild($hTreeView, $hItem, $sFile) EndIf WEnd ; Close search handle FileClose($hSearch) EndFunc ;==>ListFiles_ToTreeView
      So i am searching for the exact same function just with
      GUICtrlCreateTreeViewItem
      instead of 
      _GUICtrlTreeView_AddChild
       
      Ps: i have a folder structure where i like to color each item green if a file only exist once and red if it exist more than once.

      Could someone help?
       
      Solution:

       
       
    • 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
×
×
  • Create New...