#include once "window9.bi"

Sub DeleteItemListView(ByVal gadget As Long , ByVal iItem As long) Export
    dim as hwnd list = GadgetID(gadget)
    dim as integer iCountItems = SendMessage(list,LVM_GETITEMCOUNT,0,0)
    dim as any ptr pStackIndexes = GetProp(list, "stackindexes")
    if iCountItems = 0 then
        exit sub
    elseif iCountItems = 1 andalso iItem = 0 then
        dim as HIMAGELIST imgList = Cast(HIMAGELIST,GetHimageCombo_(gadget))
        if imgList then 
            ImageList_RemoveAll(imgList)
            freeStackFreeIndexes(pStackIndexes)
        EndIf
    else
        dim as HIMAGELIST imgList = Cast(HIMAGELIST,GetHimageCombo_(gadget))
        if imgList then
            if pStackIndexes then
                for i as Long = 0 to 500
                    dim lvcol as LVCOLUMN
                    lvcol.mask = LVCF_SUBITEM
                    if SendMessage(list,LVM_GETCOLUMN , i , cast(lparam,@lvcol)) then
                        Dim LV_ITEM_ As LV_ITEM
                        LV_ITEM_.mask=LVIF_IMAGE
                        LV_ITEM_.iItem = iItem
                        LV_ITEM_.iSubItem = i
                        if SendMessage(list,LVM_GETITEM,0,Cast(lparam,@LV_ITEM_)) then
                            if LV_ITEM_.iImage <> -2 then
                                PushStackFreeIndexes(pStackIndexes , LV_ITEM_.iImage)
                            EndIf
                        else
                            exit for
                        endif
                    else
                        exit for
                    endif
                Next
            endif
        EndIf
    EndIf
    SendMessage(list,LVM_DELETEITEM,CInt(iItem),0)
End Sub
