Jump to content
Sign in to follow this  
ReFran

Treeview Read to / from File

Recommended Posts

ReFran

Here an example how

- to read treeview items from file or save to.

- Move treeview items up/down - left/right

Enjoy, Reinhard

edit: Inserted _GUICtrlTreeView_Begin../..EndUpdate($hTree)

;; ReFran (3.2.12.1)
;; Move treeview items up/down - left/right
;; Read treeview items from file or save to.


#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GUITreeView.au3>
#include <StaticConstants.au3>

DIM $Childs
DIM $TvFileNm = @scriptDir &"\" &"TvItems.txt"

$hMain = GUICreate("Treeview", 342, 183, 341, 278, -1798701056, 256)

$hTree = GUICtrlCreateTreeView(6, 6, 200, 150, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS), $WS_EX_CLIENTEDGE)
$generalitem = GUICtrlCreateTreeViewItem("General", $hTree)
    $aboutitem = GUICtrlCreateTreeViewItem("About", $generalitem)
    $compitem = GUICtrlCreateTreeViewItem("Computer", $generalitem)
    $useritem = GUICtrlCreateTreeViewItem("User", $generalitem)
    $useritem1 = GUICtrlCreateTreeViewItem("1.User", $useritem)
    $useritem2 = GUICtrlCreateTreeViewItem("2.User", $useritem)
    $compitem3 = GUICtrlCreateTreeViewItem("Schmuser", $generalitem)
    $compitem4 = GUICtrlCreateTreeViewItem("Looser", $generalitem)
    $displayitem = GUICtrlCreateTreeViewItem("Display", $hTree)
    $resitem = GUICtrlCreateTreeViewItem("Resolution", $displayitem)
    $otheritem = GUICtrlCreateTreeViewItem("Other", $displayitem)

$Btn_TvToFile = GUICtrlCreateButton("To file", 240, 10, 50, 25)
$Btn_TvClear = GUICtrlCreateButton("Clear", 240, 37, 50, 25)
$Btn_TvFromFile = GUICtrlCreateButton("From file", 240, 65, 50, 25)

$Btn_up = GUICtrlCreateButton("Up", 240, 110, 25, 25)
$Btn_dn = GUICtrlCreateButton("Dn", 270, 110, 25, 25)
$Btn_left = GUICtrlCreateButton("<-", 240, 140, 25, 25)
$Btn_right = GUICtrlCreateButton("->", 270, 140, 25, 25)

GUICtrlSetState($generalitem, BitOR($GUI_EXPAND, $GUI_DEFBUTTON))  ; Expand the "General"-item and paint in bold
GUICtrlSetState($displayitem, BitOR($GUI_EXPAND, $GUI_DEFBUTTON))  ; Expand the "Display"-item and paint in bold

    
GUISetState()


While 1 
    $Msg = GUIGetMsg()
    Switch $Msg
        Case $GUI_EVENT_CLOSE
            Exit
        
;; TV Read from / Write to example
        Case $Btn_TvToFile
            $item = _GUICtrlTreeView_GetFirstItem($hTree)
             If $item = 0 Then
                MsgBox(64, "Treeview", "No item in TV")
             Else
                TV_Save()
             endif
        Case $Btn_TvClear
            _GuiCtrlTreeView_DeleteAll($hTree)
        Case $Btn_TvFromFile
            TV_ReadData()
            
;; TV move items example
        Case $Btn_Up
            TV_ItemMove("up")
            
        Case $Btn_Dn
            TV_ItemMove("dn")
                    
        Case $Btn_left
            TV_ItemMove("left")
            
        Case $Btn_right
            TV_ItemMove("right")
                    
    EndSwitch
    Sleep(10)
WEnd
    
#Region TVMoveItems
Func TV_ItemMove($dir);;; Moves Treeview  Items
    $item = _GuiCtrlTreeView_GetSelection($hTree)
    If $item = 0 Then
        MsgBox(64, "Treeview", "No item currently selected")
        Return;;==> return from function
    Endif
    
    $itemText = _GuiCtrlTreeView_GetText($htree,$item)
    $itemPA = _GUICtrlTreeView_GetParentHandle($hTree, $item)
            
;; check and prepare to move up
    if $dir = "up" then ;;new UdF - item will be inserted after, so get prev of prev
        $itemToPrev = _GuiCtrlTreeView_GetPrevSibling($hTree, $item)
        $itemTo = _GuiCtrlTreeView_GetPrevSibling($hTree, $itemToPrev)
        if $itemPa = 0 then;; get the first sibiling item 
            $itemFirstSib = _GUICtrlTreeView_GetFirstItem($hTree)
        Else
            $itemFirstSib = _GUICtrlTreeView_GetFirstChild($hTree,$itemPA)
        endif
        if $itemFirstSib = $item Then;; first item can not moved up
            msgbox(0,"","Moving up is not possible")
            Return;;==> return from function
        EndIf
    endif
                
;; check and prepare to move down
    if $dir = "dn" then; item will be inserted after, so get next
        $itemTo = _GuiCtrlTreeView_GetNextSibling($hTree,$item)
        $itemToNext = _GuiCtrlTreeView_GetNextSibling($hTree,$item)
        $itemLastSib =_GuiCtrlTreeView_GetLastChild($hTree,$itemPA) 
        if $itemLastSib = $item or $itemto = 0 Then
            msgbox(0,"","Moving down not possible")
            return;;==> return from function
        endif
    endif
            
;; check and prepare to move left
    if $dir = "left" then
        $itemLevel = _GuiCtrlTreeView_Level($hTree, $item)
        $itemLevelPa = $itemLevel - 1
        if $itemLevel = 0 then
            msgbox(0,"","Not possible")
            return;;==> return from function
        endif
        $itemTo = $itemPa
    endif
        
;; check and prepare to move right  
    if $dir = "right" then
        $itemLevel = _GuiCtrlTreeView_Level($hTree, $item)
        if $itemlevel = 0 then
            $itemFirstSib = _GUICtrlTreeView_GetFirstItem($hTree)
        Else
            $itemFirstSib = _GUICtrlTreeView_GetFirstChild($hTree,$itemPa)
        endif
        if $item = $itemFirstSib Then
            msgbox(0,"","Not possible")
            return;;==> return from function
        endif
        $itemTo = _GuiCtrlTreeView_GetPrevSibling($hTree,$item)
    endif

;;check for children and get it
    $itemIsParent = _GUICtrlTreeView_GetChildCount($hTree, $item);0 = no childs
    if $itemIsParent > 0 then 
        $itemLevel = _GuiCtrlTreeView_Level($hTree, $item)
        $Childs = GetTree($item,$itemLevel,$itemText)
    endif
        
;; start to move
    if $dir = "dn" and $itemToNext = $itemLastSib then
        $itemInsert = _GuiCtrlTreeView_Add($hTree, $item, $itemText)
    elseif $dir = "up" and $itemToPrev = $itemFirstSib then
        $itemInsert = _GuiCtrlTreeView_AddFirst($hTree, $itemtoPrev, $itemText)
    elseif $dir = "right" then
        $itemInsert = _GuiCtrlTreeView_AddChild($hTree,$itemto,$itemtext)
    elseif $dir = "left" and $itemLevelPa > 0 Then
        $itemPaPa = _GUICtrlTreeView_GetParentHandle($hTree, $itemPA)
        $itemInsert = _GUICtrlTreeView_InsertItem($htree,$itemText,$itemPaPa,$itemPA)
    else
        if $dir = "left" then $itemPa = 0
        $itemInsert = _GUICtrlTreeView_InsertItem($htree,$itemText,$itemPA,$itemTo)  
    endif
    _GuiCtrlTreeView_Delete($htree,$item)
    
;; add Childs from selected Item to Inserted Item
    if $itemIsParent > 0 then   
;msgbox(0,"",$childs)
        Dim $hNode[50]
        $aChilds = StringSplit($Childs, ";")
        for $i = 1 to $achilds[0]-1
            $level = $aChilds[$i]
            $itemText =  $aChilds[$i+1]
            if $level = $itemLevel then 
                $hNode[$level] = $itemInsert
                else            
                $hNode[$level]= _GuiCtrlTreeView_AddChild($hTree, $hNode[$level-1],$itemText )
            endif
            $i +=1
        next
        $Childs=""
    endif
    
    _GuiCtrlTreeView_SelectItem($hTree, $itemInsert)    
                
EndFunc
        
func GetTree($item,$itemLevel,$itemText);; Get the Tree for Children
    $itemLevelPa = $itemLevel 
    Do
        $Childs &= $itemLevel &";" &$itemText &";"; &@lf
        $item = _GuiCtrlTreeView_GetNext($hTree, $item)
        $itemLevel = _GuiCtrlTreeView_Level($hTree, $item)
        $itemText = _GuiCtrlTreeView_GetText($htree,$item)
    until $itemLevel <= $itemLevelPa; or $itemText = 0
;MsgBox(0,"",$Childs)
    return $childs
endfunc

#EndRegion TVMoveItems


#Region TvReadWrite
;;;; Save Treeview Items to file;;;;;
func TV_Save();;# added for better reading, one space is added for level
$item = _GUICtrlTreeView_GetFirstItem($hTree)
         $TvList = ""
         Do 
             $itemText = _GuiCtrlTreeView_GetText($htree, $item)
             $itemlevel = _GuiCtrlTreeView_Level($htree, $item)
             $itemSpace = stringleft("                  ",$itemlevel)
             $TvList &= $itemSpace &"#" & $itemText &@LF
             $item = _GuiCtrlTreeView_GetNext($htree, $item)
        until $item = 0  
        msgBox(0,$TvFileNM,$TvList)
        $FileId = Fileopen($TvFileNM,2)
        FileWrite($FileId,StringstripWS($TvList,2))
        FileClose($FileId)
endfunc;;=> TVSave

;;;; Read Treeview Items from file;;;;;
;;  # is used as start sign (will not diplayed), space is used for level;;
Func TV_ReadData();; Read Data from a TvFile (# as start sign, space for level
    if not FileExists($TvFileNm) Then
        MsgBox(0,"","File with TV-Items don't exists!")
    EndIf
    Dim $aTree = StringSplit(FileRead($TvFileNM, FileGetSize($TvFileNM)), @LF)
    TV_Display($aTree)
endFunc

Func TV_Display($aTree);; Read from variable to TV
    Dim $hNode[50];Keep Parent on Level
    _GUICtrlTreeView_BeginUpdate($hTree)
                For $i = 1 to $aTree[0]
        $line = StringStripCR($aTree[$i])
;msgbox(0,"",$line)
        $level = StringInStr($line, "#")
        if $level = 0 then exitloop
        if $level = 1 then
          $hNode[$level] =_GuiCtrlTreeView_Add($hTree, 0, StringMid($line, $level+1))
        Else
          $hNode[$level]= _GuiCtrlTreeView_AddChild($hTree, $hNode[$level-1], StringMid($line, $level+1)) 
        Endif
    Next
                _GUICtrlTreeView_EndUpdate($hTree)
EndFunc;;==>End of TV_ReadData

#endRegion TvReadWrite
Edited by ReFran

Share this post


Link to post
Share on other sites
Garp99HasSpoken

Looks interesting but an example of TvItems.txt would be more helpful :mellow:

Share this post


Link to post
Share on other sites
ReFran

Looks interesting but an example of TvItems.txt would be more helpful :mellow:

For that I put the button "To file" in the GUI. So you may use that and then look into "TvItems.txt" to see how this file should look like. Then you may write your own TvItemstext, cut the basic Treeview items, and put a function call at this place.

Best regards, Reinhard

PS: Working with old examples I saw that I lost some basic ....ini, .....txt or .....csv files, which are needed to run the example. So I decided to put it directly in.

Share this post


Link to post
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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.