Jump to content

How to make a GUICtrlCreateTreeView from a specific data?


Recommended Posts

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?

 

#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)

 

Link to comment
Share on other sites

Might not be the best way but it should do the trick.

#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)

 

Link to comment
Share on other sites

Using recursive algorithm :

#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

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...