Sign in to follow this  
Followers 0
qingtianyu9

[Solved use GetNext loop]Delete checked items in treeview

3 posts in this topic

#1 ·  Posted (edited)

How to delete the checked item in treeview?

Items come from another ListView1, I create the TreeView1 by this way:

$SelectedIndices = _GUICtrlListView_GetSelectedIndices($ListView1, True)
If IsArray($SelectedIndices) Then
    For $i = 1 to $SelectedIndices[0]
        $WinTitle = _GUICtrlListView_GetItemText($ListView1, Int($SelectedIndices[$i]))
        _GUICtrlTreeView_Add($TreeView1, 0, $WinTitle)
    Next
Else
    ;do nothing
EndIf

Now,Problem is delete the checked item from treeview1, but i unknow the Handle/Control ID of the checked item.

I want multiple-delete items from treeview by checkedboxs.

help me, thanks!

My stupid solved method:

#include <GuiConstantsEx.au3>
#include <GuiTreeView.au3>
#include <GuiImageList.au3>
#include <WindowsConstants.au3>
#include <GuiTreeView.au3>
#include <TreeViewConstants.au3>

Opt("GUIOnEventMode", True)

;GUI Beging
Global $hItem, $hImage, $iImage, $hTreeView
Global $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_CHECKBOXES)

GUICreate("TreeView Delete", 400, 300)
GUICtrlCreateButton("delete", 400-50, 300-25, 48, 21, $WS_GROUP)
GUICtrlSetOnEvent (-1, "_Del_checked_items_from_treeview")
$hTreeView = GUICtrlCreateTreeView(2, 2, 396, 268, $iStyle, $WS_EX_CLIENTEDGE)
GUISetState()
;GUI End

;exit programe event
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

;image list
$hImage = _GUIImageList_Create(16, 16, 5, 3)
_GUIImageList_AddIcon($hImage, "shell32.dll", 110)
_GUIImageList_AddIcon($hImage, "shell32.dll", 131)
_GUIImageList_AddIcon($hImage, "shell32.dll", 165)
_GUIImageList_AddIcon($hImage, "shell32.dll", 168)
_GUIImageList_AddIcon($hImage, "shell32.dll", 137)
_GUIImageList_AddIcon($hImage, "shell32.dll", 146)
_GUICtrlTreeView_SetNormalImageList($hTreeView, $hImage)

;create treeview children
_GUICtrlTreeView_BeginUpdate($hTreeView)
For $x = 1 To Random(2, 10, 1)
    $iImage = Random(0, 5, 1)
    $hItem = _GUICtrlTreeView_Add($hTreeView, 0, StringFormat("[%02d] New Item", $x), $iImage, $iImage)
    For $y = 1 To Random(2, 10, 1)
        $iImage = Random(0, 5, 1)
        _GUICtrlTreeView_AddChild($hTreeView, $hItem, StringFormat("[%02d] New Child", $y), $iImage, $iImage)
    Next
Next
_GUICtrlTreeView_EndUpdate($hTreeView)


;main
While 1
    Sleep(10)
WEnd


func _Del_checked_items_from_treeview()
    Local $Count = _GUICtrlTreeView_GetCount($hTreeView) ;get treeview count
    Local $arr[1] ;treeview items handle array
    Local $Item1, $ItemN, $ItemP;first item, current item, previous item
    Local $i = 1 ;iterator
    If $Count > 0 Then
        ReDim $arr[$Count]
        $Item1 = _GUICtrlTreeView_GetFirstItem($hTreeView) ;get first item handle
        $ItemN = $Item1 ;current item
        $ItemP = $Item1 ;previous item
        ;get handle of item and save it to array
        For $i = 1 to $Count
            If $i = 1 Then
                $arr[$i-1] = $Item1 ;save first item handle to array
            Else
                $ItemN = _GUICtrlTreeView_GetNext($hTreeView, $ItemP) ;get current item handle by previous item handle
                $ItemP = $ItemN ;save current item handle to temporary variable(change previous item handle to current item handle)
                $arr[$i-1] = $ItemN ;save current item handle to array
            EndIf
        Next
        ;check item ischecked,if checked then delete it
        FOR $iElement IN $arr
            If _GUICtrlTreeView_GetChecked($hTreeView, $iElement) Then _GUICtrlTreeView_Delete($hTreeView, $iElement)
        Next
    EndIf
EndFunc

Func _Exit()
    Exit
EndFunc

This method is stupid.

Anybody can improve this method, thanks!

Edited by qingtianyu9

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

How are you checking which item is selected in the TreeView? For example _GUICtrlTreeView_GetSelection() returns the item handle and that's all you need to delete it.

Your posted code gets an array of selected items from a ListView, gets the item text from the ListView, ... and then you want to delete some item from the TreeView?

:graduated:

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

How are you checking which item is selected in the TreeView? For example _GUICtrlTreeView_GetSelection() returns the item handle and that's all you need to delete it.

Your posted code gets an array of selected items from a ListView, gets the item text from the ListView, ... and then you want to delete some item from the TreeView?

:graduated:

Yes, You said that was right, that is i wants.

I want multiple-delete items from treeview by checkedboxs.

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  
Followers 0