Burgs Posted January 27, 2019 Posted January 27, 2019 (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 January 27, 2019 by Burgs
LarsJ Posted January 27, 2019 Posted January 27, 2019 You need a little more code: expandcollapse popup#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 Controls, File Explorer, ROT objects, UI Automation, Windows Message MonitorCompiled code: Accessing AutoIt variables, DotNet.au3 UDF, Using C# and VB codeShell menus: The Context menu, The Favorites menu. Shell related: Control Panel, System Image ListsGraphics related: Rubik's Cube, OpenGL without external libraries, Navigating in an image, Non-rectangular selectionsListView controls: Colors and fonts, Multi-line header, Multi-line items, Checkboxes and icons, Incremental searchListView controls: Virtual ListViews, Editing cells, Data display functions
Burgs Posted February 14, 2019 Author Posted February 14, 2019 (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 February 14, 2019 by Burgs
FrancescoDiMuro Posted February 15, 2019 Posted February 15, 2019 14 hours ago, Burgs said: if Int($_HIERARCHY[$_populate]) == 0 Then Since you are using this operator (==), it will traduced in: "SomeValue" == "0" so, better use a single = sign Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette
Burgs Posted February 15, 2019 Author Posted February 15, 2019 haha yes I know...I always do that out of habit...but I've never had any problems with it...thanks for posting
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now