jcpetu Posted August 27, 2020 Posted August 27, 2020 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: expandcollapse popup#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
Nine Posted August 27, 2020 Posted August 27, 2020 (edited) Recursion is the key of simplicity. expandcollapse popup#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 August 27, 2020 by Nine jcpetu 1 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy
jcpetu Posted August 27, 2020 Author Posted August 27, 2020 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
Nine Posted August 27, 2020 Posted August 27, 2020 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. “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy
jcpetu Posted August 27, 2020 Author Posted August 27, 2020 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
Nine Posted August 27, 2020 Posted August 27, 2020 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. “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy
jcpetu Posted August 27, 2020 Author Posted August 27, 2020 I modified the input by StringFormat("%03d", $key)
Nine Posted August 27, 2020 Posted August 27, 2020 Yep, that is the way to do it. “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy
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