Jump to content

How to get the handle of the LAST item in a Tree View?


Recommended Posts

I have a program that deals with disk directories, and occasionally I need to remove from my list those folder(s) that have no files in them. 

To do this, I start at the end of my TreeView and move back toward the beginning and delete any items that have no "Children" (and are not themselves files).  This works fine, but it would be a bit faster and cleaner if I had a good way to get the LAST item's handle in a TreeView (as opposed to how I now do it - by looping through the total number of items and repeatedly getting the _GUICtrlTreeView_GetNext ...)

Q ===> I see that there is a UDF that gets me the handle to the FIRST item in a TreeView (_GUICtrlTreeView_GetFirstItem), but I see no function to get the LAST item in a TreeView.  Am I cross-eyed and just missing it???  Or is there a cleaner faster way than what I do above??

 

Func Check_For_And_Remove_Child_Less_Branches($h_Tree_View)
    $Total_Number_Of_Tree_View_Items  = _GUICtrlTreeView_GetCount($h_Tree_View)
    $Next_Tree_View_Item_Handle = _GUICtrlTreeView_GetFirstItem($h_Tree_View)
    For $i = 1 to $Total_Number_Of_Tree_View_Items                                               ; get to END of Tree View and get last items handle
        $Current_Tree_View_Item_Handle = $Next_Tree_View_Item_Handle
        $Next_Tree_View_Item_Handle = _GUICtrlTreeView_GetNext($h_Tree_View, $Current_Tree_View_Item_Handle)  ; gets the next items handle in the Tree View based on the current items handle
    Next
    ;  $Current_Tree_View_Item_Handle is now at the last item in the Tree View List
    For $i = 1 to $Total_Number_Of_Tree_View_Items                                               ; step through all Tree View items to find non-file entries
        $Tree_View_Text = _GUICtrlTreeView_GetText($h_Tree_View, $Current_Tree_View_Item_Handle) ; get the Tree View List items Full Path text
        $Prev_Tree_View_Item_Handle = _GUICtrlTreeView_GetPrev($h_Tree_View, $Current_Tree_View_Item_Handle)  ; gets the prev items handle in the Tree View based on the current items handle
        If NOT StringInStr($Tree_View_Text, ".") Then                                            ; dont loop if it IS a file like a .mp4....
            $Number_Of_Children = _GUICtrlTreeView_GetChildCount($h_Tree_View, $Current_Tree_View_Item_Handle)
            ;MsgBox("", "", " $Tree_View_Text = " & $Tree_View_Text & "     $Number_Of_Children = " & $Number_Of_Children)
            If $Number_Of_Children < 1 Then
                _GUICtrlTreeView_Delete($h_Tree_View, $Current_Tree_View_Item_Handle)
            EndIf
        EndIf
        $Current_Tree_View_Item_Handle = $Prev_Tree_View_Item_Handle
    Next
EndFunc

 

Edited by Know_Fear
Link to comment
Share on other sites

Know_Fear,

See if this works for you
This walks through all the expanded tree in view ... edited

Func _Delete_empty_folders(ByRef $h_Tree_View, $hItem)
    Local $Text, $pre, $hprevious, $hchild
    Local $hItem1 = _GUICtrlTreeView_GetNextSibling($h_Tree_View, $hItem)

    While $hItem <> $hItem1
        $Text = _GUICtrlTreeView_GetText($h_Tree_View, $hItem)
        If $pre = 1 And $Text <> "" Then
            $pre = StringReplace(_GUICtrlTreeView_GetTree($h_Tree_View, $hprevious), "|", "\")
            $hchild = _GUICtrlTreeView_GetFirstChild($h_Tree_View, $hprevious)

            If Not $hchild And StringInStr(FileGetAttrib($pre), "D") Then
                $hItem = _GUICtrlTreeView_GetNextSibling($h_Tree_View, $hprevious)
                ConsoleWrite($pre & @CRLF)
                _GUICtrlTreeView_Delete($h_Tree_View, $hprevious)
            EndIf
        EndIf

        If Not $Text Then
            $pre = 0
        ElseIf Not StringInStr($Text, ".") Then
            $pre = 1
            $hprevious = $hItem
        EndIf
        $hItem = _GUICtrlTreeView_GetNext($h_Tree_View, $hItem)
    WEnd
EndFunc   ;==>_Delete_empty_folders

 

Edited by Deye
Link to comment
Share on other sites

Thank you Deye.  Interesting how many ways there are to skin a cat!

 

However as I said, mine also works just fine - but I was wondering if there is a better way to get the handle of the last item in a TreeView rather than the way I did it in the first 5 lines of the example I posted above.

Link to comment
Share on other sites

2 hours ago, Know_Fear said:

but I was wondering if there is a better way to get the handle of the last item in a TreeView

Not sure , maybe like this  :

Local $hLast
    $hItem = _GUICtrlTreeView_GetFirstItem($h_Tree_View)
    While $hItem <> 0
         $hLast = $hItem
        $hItem = _GUICtrlTreeView_GetNext($h_Tree_View, $hItem)
    WEnd
    $hLast = _GUICtrlTreeView_GetPrev($h_Tree_View, $hLast)
    MsgBox(0, "last", _GUICtrlTreeView_GetText($h_Tree_View, $hLast))

 

Link to comment
Share on other sites

why not just specify the index of the last item?

;_GUICtrlTreeView_GetItemByIndex($hWnd, $hItem, $iIndex)

_GUICtrlTreeView_GetItemByIndex($h_Tree_View,$Next_Tree_View_Item_Handle,$Total_Number_Of_Tree_View_Item);;maybe ($Total_Number_Of_Tree_View_Item - 1)

 

Link to comment
Share on other sites

I think my example above is probably faster - It doesn't process an entry in the tree View if it contains a "." (i.e, if it is a file, as opposed to a directory). 

 

OTH, I do step through each and every item in the Tree View (in reverse order). 

 

If it doesn't have a ".", then it's a Directory, and then I see if it has any children.  If not, whack it!

 

But whatever the case, I'm grateful for the answers from a different perspective, and the additional info !

Edited by Know_Fear
Link to comment
Share on other sites

Know_Fear, 

you are welcome :)

  Using what you have posted ( just tried it out ) and using this UDF which i was testing the function on wont  play along  ..

the edit i had in place : (for anyone wanting to test ..)
           ; If file scroll selected AND files were found AND the branch is ..........
           ; Scroll to first file in this branch
           _CFF_File_Visible($g_hCFF_TreeView, $aFill_Ret)
           EndIf

                    _Delete_empty_folders($g_hCFF_TreeView, $hItem)

 

Deye

 

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...