Jump to content

Recommended Posts

Posted (edited)

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
Posted

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

 

  • 3 weeks later...
Posted (edited)

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

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