Jump to content
Sign in to follow this  
ReFran

Treeview - move tv-item up/down left/right

Recommended Posts

ReFran

Does someone already have an example how to:

move an treeview item

up, down and (or)

left, right

using according button clicks?

Best regards, Reinhard

Edited by ReFran

Share this post


Link to post
Share on other sites
ReFran

Yea,

from time to time it's more hard to rebuild something (A3lib) instead of writing new.

Improvements are welcome.

Best regards, Reinhard

;; ReFran (3.2.12.1)
;; Move treeview items up/down - left/right

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

DIM $Childs


$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_up = GUICtrlCreateButton("Up", 240, 80, 25, 25)
$Btn_dn = GUICtrlCreateButton("Dn", 270, 80, 25, 25)
$Btn_left = GUICtrlCreateButton("<-", 240, 110, 25, 25)
$Btn_right = GUICtrlCreateButton("->", 270, 110, 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
        
        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
    

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 
        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 of 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
Edited by ReFran

Share this post


Link to post
Share on other sites
rasim

Nice ;)

Share this post


Link to post
Share on other sites
ReFran

Nice ;)

Thanks.

It is a part of a bigger script for writing bookmarks into a PDF.

By time I will look into

the Drag and Draop study from Siao:

http://www.autoitscript.com/forum/index.ph...st&p=427856

(study because it doesn't work for longer treeviews with scrollbars)

and shurly Siao and your examples for edit items:

http://www.autoitscript.com/forum/index.ph...st&p=502959

Best regards, Reinhard

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  

×