DannyJ Posted July 5, 2021 Posted July 5, 2021 Hello, I have a specific data, and I am thinking how to make from this data a TreeView. I would be grateful if you could me provide some hints or an algorithm. The data looks like this: Org/Unit1/Users Org/Unit1/Computers Org/Unit1/Computers/Wifi1 Org/Unit1/Computers/Wifi2 Org/Unit1/Computers/Guest/Wifi1 Org/Unit2/Company/Users Org/Unit2/Company/Tables Org/Unit2/Computers Do you have any idea or hint how to make a TreeView from this data? expandcollapse popup#include-once #include <GUIConstantsEx.au3> #include <TreeViewConstants.au3> #include <WindowsConstants.au3> Local $hGUI = GUICreate("ControlTreeView Example", 212, 212) Local $idTreeView_1 = GUICtrlCreateTreeView(6, 6, 200, 160, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_CHECKBOXES), $WS_EX_CLIENTEDGE) Local $hTreeView_1 = ControlGetHandle($hGUI, "", $idTreeView_1) Local $arr[] = [ "Org/Unit1/Users", _ "Org/Unit1/Computers", _ "Org/Unit1/Computers/Wifi1", _ "Org/Unit1/Computers/Wifi2", _ "Org/Unit1/Computers/Guest/Wifi1", _ "Org/Unit2/Company/Users", _ "Org/Unit2/Company/Tables", _ "Org/Unit2/Computers"] Local $idRoot = GUICtrlCreateTreeViewItem("Root", $idTreeView_1) GUICtrlCreateTreeViewItem("x", $idRoot) GUICtrlCreateTreeViewItem("Item 2", $idRoot) GUICtrlCreateTreeViewItem("Item 3", $idRoot) Local $idItem_4 = GUICtrlCreateTreeViewItem("Item 4", $idRoot) GUICtrlCreateTreeViewItem("Item 4.1 | Item 4.1.1 ", $idItem_4) GUICtrlCreateTreeViewItem("Item 4.2", $idItem_4) GUICtrlCreateTreeViewItem("Item 5", $idRoot) GUISetState(@SW_SHOW, $hGUI) ControlTreeView($hGUI, "", $hTreeView_1, "Expand", "Root") ;ControlTreeView($hGUI, "", $hTreeView_1, "Exists", "Root|Item 4") ;ControlTreeView($hGUI, "", $hTreeView_1, "Check", "Root|Item 4") ;ControlTreeView($hGUI, "", $hTreeView_1, "Select", "Root|Item 4") ;ControlTreeView($hGUI, "", $hTreeView_1, "Expand", "Root|Item 4") While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop EndSwitch WEnd GUIDelete($hGUI)
Luke94 Posted July 5, 2021 Posted July 5, 2021 Might not be the best way but it should do the trick. expandcollapse popup#include-once #include <GUIConstantsEx.au3> #include <TreeViewConstants.au3> #include <WindowsConstants.au3> #include <StringConstants.au3> #include <GuiTreeView.au3> Local $hGUI = GUICreate("ControlTreeView Example", 212, 212) Local $idTreeView_1 = GUICtrlCreateTreeView(6, 6, 200, 160, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_CHECKBOXES), $WS_EX_CLIENTEDGE) Local $hTreeView_1 = ControlGetHandle($hGUI, "", $idTreeView_1) Local $arr[] = [ "Org/Unit1/Users", _ "Org/Unit1/Computers", _ "Org/Unit1/Computers/Wifi1", _ "Org/Unit1/Computers/Wifi2", _ "Org/Unit1/Computers/Guest/Wifi1", _ "Org/Unit2/Company/Users", _ "Org/Unit2/Company/Tables", _ "Org/Unit2/Computers"] Opt("GUIDataSeparatorChar", "/") ; Need for FindItemEx For $i = 0 To (UBound($arr) - 1) Step 1 ; Loop through $arr Local $aStringSplit = StringSplit($arr[$i], '/', $STR_NOCOUNT) ; Split each line using / delimiter Local $sCurrentPath = '' For $j = 0 To (UBound($aStringSplit) - 1) Step 1 ; Loop through StringSplit above If $j = 0 Then ; If it's the first item, using Add, otherwise AddChild If _GUICtrlTreeView_FindItem($hTreeView_1, $aStringSplit[$j]) = 0 Then ; If we can't find the current item, add it _GUICtrlTreeView_Add($hTreeView_1, 0, $aStringSplit[$j]) EndIf Else $hItem = _GUICtrlTreeView_FindItemEx($hTreeView_1, $sCurrentPath) ; Find the item we're adding a child to (which will have been added either above or in the previous loop If _GUICtrlTreeView_FindItemEx($hTreeView_1, $sCurrentPath & '/' & $aStringSplit[$j]) = 0 Then ; Check if the new item exists, if not, add child to above item _GUICtrlTreeView_AddChild($hTreeView_1, $hItem, $aStringSplit[$j]) ; add here EndIf EndIf If $sCurrentPath = '' Then $sCurrentPath &= $aStringSplit[$j] ; Updates current path for next loop Else $sCurrentPath &= '/' & $aStringSplit[$j] ; Updates current path for next loop EndIf ;~ ConsoleWrite($sCurrentPath & @CRLF) Next Next ;~ Local $idRoot = GUICtrlCreateTreeViewItem("Root", $idTreeView_1) ;~ GUICtrlCreateTreeViewItem("x", $idRoot) ;~ GUICtrlCreateTreeViewItem("Item 2", $idRoot) ;~ GUICtrlCreateTreeViewItem("Item 3", $idRoot) ;~ Local $idItem_4 = GUICtrlCreateTreeViewItem("Item 4", $idRoot) ;~ GUICtrlCreateTreeViewItem("Item 4.1 | Item 4.1.1 ", $idItem_4) ;~ GUICtrlCreateTreeViewItem("Item 4.2", $idItem_4) ;~ GUICtrlCreateTreeViewItem("Item 5", $idRoot) GUISetState(@SW_SHOW, $hGUI) ;~ ControlTreeView($hGUI, "", $hTreeView_1, "Expand", "Root") ControlTreeView($hGUI, "", $hTreeView_1, "Expand", "Org") ;ControlTreeView($hGUI, "", $hTreeView_1, "Exists", "Root|Item 4") ;ControlTreeView($hGUI, "", $hTreeView_1, "Check", "Root|Item 4") ;ControlTreeView($hGUI, "", $hTreeView_1, "Select", "Root|Item 4") ;ControlTreeView($hGUI, "", $hTreeView_1, "Expand", "Root|Item 4") While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop EndSwitch WEnd GUIDelete($hGUI)
Nine Posted July 5, 2021 Posted July 5, 2021 Using recursive algorithm : expandcollapse popup#include <Constants.au3> #include <GUIConstants.au3> #include <GuiTreeView.au3> Local $hGUI = GUICreate("ControlTreeView Example", 212, 212) Local $idTreeView_1 = GUICtrlCreateTreeView(6, 6, 200, 160, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_CHECKBOXES), $WS_EX_CLIENTEDGE) Local $hTreeView_1 = ControlGetHandle($hGUI, "", $idTreeView_1) Local $arr[] = [ _ "Org/Unit1/Users", _ "Org/Unit1/Computers", _ "Org/Unit1/Computers/Wifi1", _ "Org/Unit1/Computers/Wifi2", _ "Org/Unit1/Computers/Guest/Wifi1", _ "Org/Unit2/Company/Users", _ "Org/Unit2/Company/Tables", _ "Org/Unit2/Computers", _ "Org2/Unit1/Users", _ "Org2/Unit1/Computers", _ "Org2/Unit1/Computers/Wifi1", _ "Org2/Unit1/Computers/Wifi2", _ "Org2/Unit1/Computers/Guest/Wifi1", _ "Org2/Unit2/Company/Users", _ "Org2/Unit2/Company/Tables", _ "Org2/Unit2/Computers"] GUISetState() Create_Tree($hTreeView_1, "Root") ControlTreeView($hGUI, "", $hTreeView_1, "Expand", "Root") While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop EndSwitch WEnd Func Create_Tree($hTVC, $sRoot) Local $hRoot = _GUICtrlTreeView_Add($hTVC, 0, $sRoot) Global $iIndx = 0, $aPart While $iIndx < UBound($arr) $aPart = StringSplit($arr[$iIndx], "/", $STR_NOCOUNT) Recurse_Tree($hTVC, 0, $hRoot) WEnd EndFunc ;==>Create_Tree Func Recurse_Tree($hTVC, $iPart, $hParent) If $iIndx = UBound($arr) Then Return If $iPart >= UBound($aPart) Then $iIndx += 1 If $iIndx = UBound($arr) Then Return $aPart = StringSplit($arr[$iIndx], "/", $STR_NOCOUNT) Return EndIf Local $sPart = $aPart[$iPart] Local $hChild = _GUICtrlTreeView_AddChild($hTVC, $hParent, $sPart) While $iIndx < UBound($arr) And $iPart < UBound($aPart) And $sPart = $aPart[$iPart] Recurse_Tree($hTVC, $iPart + 1, $hChild) WEnd EndFunc ;==>Recurse_Tree robertocm 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) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
DannyJ Posted July 12, 2021 Author Posted July 12, 2021 (edited) Thank you very much your help @Luke94 and @Nine Edited July 12, 2021 by DannyJ
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