Jump to content
Sign in to follow this  
Lupo73

Delete selected ListView items

Recommended Posts

Lupo73

The problem is: I have two ListViews, the user can select items in one of the list and clicking a button can remove items selected from the list.

Now, I need that when an item is removed from a ListView, the corresponding item is removed from the other ListView.

I thought to use functions like _GUICtrlListView_DeleteItemsSelected, _GUICtrlListView_GetItemSelected, _GUICtrlListView_SetItemSelected... but I don't know how to use them together.. and in particular how to select in the second list the corresponding items of the first list.

Can someone help me? Thanks!


SFTPEx, AutoCompleteInput_DateTimeStandard(), _ImageWriteResize()_GUIGraduallyHide(): some AutoIt functions.

Lupo PenSuite: all-in-one and completely free selection of portable programs and games.

DropIt: a personal assistant to automatically manage your files.

ArcThemALL!: application to multi-archive your files and folders.

Share this post


Link to post
Share on other sites
Authenticity

Both item have exact same name? or the line number supposed to be exact?

Share this post


Link to post
Share on other sites
Lupo73

Items are not the same, but for each file my software load, in one listview are showed file name, directory and other 2 subitems for each line... and in the other listview are stored other info of that file. So 2 lists with the same number of lines, because for each file loaded are created one line in the first listview and one line in the second listview.

So if I remove a line from a listview, I need to remove the corresponding line (that may be have the same number..but I don't know how to find this "number" of the line) in the other listview.

I know this is not the best way to manage files in listview, but I don't know how to keep for each file its info (name, dir...) all together.. and show in the listview only some of that info, but not all..


SFTPEx, AutoCompleteInput_DateTimeStandard(), _ImageWriteResize()_GUIGraduallyHide(): some AutoIt functions.

Lupo PenSuite: all-in-one and completely free selection of portable programs and games.

DropIt: a personal assistant to automatically manage your files.

ArcThemALL!: application to multi-archive your files and folders.

Share this post


Link to post
Share on other sites
Authenticity

If you don't use the LPARAM of the LV_ITEM struct of the item you can give both same increasing counter and call _GUICtrlListView_FindItem and _GUICtrlListView_DeleteItem with the return value (all this upon item deletion in the first listview control) if applicable.

Share this post


Link to post
Share on other sites
rover

If you don't use the LPARAM of the LV_ITEM struct of the item you can give both same increasing counter and call _GUICtrlListView_FindItem and _GUICtrlListView_DeleteItem with the return value (all this upon item deletion in the first listview control) if applicable.

only if the OP uses UDF add listview item functions and not the internal (previous versions of his script do) GuiCtrlCreateListViewItem()

as it uses lparam of LVITEM structure to store a controlID so each item becomes a pseudo control (not explained in help file)

removing it will cause a memory leak with each GuiCtrlCreateListViewItem() item lparam changed.

this is the reason why deleting listview items created with GUICtrlCreateListViewItem() with the sendmessage LVM_DELETEALLITEMS

will cause a memory leak with each item deleted as the internally tracked item control is not deleted properly as with GUICtrlDelete()

you can't mix the internal GUICtrlCreateListViewItem() and UDF listview add item functions

but you can use GUICtrlCreateListView() with UDF functions without having to use _GUICtrlListView_Create()

_GUICtrlListView_AddItem()

_GUICtrlListView_AddSubItem()

_GUICtrlListView_InsertItem()

these are the wrappers for setting or getting listview item lparam struct element data

_GUICtrlListView_GetItemParam($hWnd, $iIndex)

_GUICtrlListView_SetItemParam($hWnd, $iIndex, $iParam)

Edited by rover

I see fascists...

Share this post


Link to post
Share on other sites
Lupo73

I'm sorry, but I'm not a guru of autoit and I hadn't understand what I have to do. Have I to use that functions? How? :)


SFTPEx, AutoCompleteInput_DateTimeStandard(), _ImageWriteResize()_GUIGraduallyHide(): some AutoIt functions.

Lupo PenSuite: all-in-one and completely free selection of portable programs and games.

DropIt: a personal assistant to automatically manage your files.

ArcThemALL!: application to multi-archive your files and folders.

Share this post


Link to post
Share on other sites
Lupo73

I have decide to remove the second listview and use only one listview (in addition I add a parameter to each item with this function _GUICtrlListView_SetItemParam).

Now the new problem is that I need to remove items selected when a button is clicked and update an item counter, but this operation doesn't work anymore.. items are not removed and counter not updated.

This is the code I tried to do:

Case $del
            GUICtrlSetState($go, $GUI_FOCUS)
            _GUICtrlListView_DeleteItemsSelected($hListView)
                
            _GUICtrlListView_GetItemSelected($hListView, 1)

        ; this is the old code
        ;While Not GUICtrlRead($hListView) = 0
        ;   GUICtrlDelete(GUICtrlRead($hListView))
        ;   _GUICtrlListView_SetItemSelected($hListViewH, GUICtrlRead($hListView))
        ;   GUICtrlDelete(GUICtrlRead($hListViewH))
            ; remove from the counter
        ;   $count = $count - 1
        ;   GUICtrlSetData($counter, $count)
        ;WEnd

SFTPEx, AutoCompleteInput_DateTimeStandard(), _ImageWriteResize()_GUIGraduallyHide(): some AutoIt functions.

Lupo PenSuite: all-in-one and completely free selection of portable programs and games.

DropIt: a personal assistant to automatically manage your files.

ArcThemALL!: application to multi-archive your files and folders.

Share this post


Link to post
Share on other sites
Authenticity

I'm not familiar with the _GuiCtrl* UDF library so I don't know what is the reason other than what rover explained but I do know that using pure API calls with LV_ITEM and LV_FINDITEM structures might be the solution but it'd involve more code and better understanding of doing this via the API calls.

Share this post


Link to post
Share on other sites
rover

I have decide to remove the second listview and use only one listview (in addition I add a parameter to each item with this function _GUICtrlListView_SetItemParam).

Now the new problem is that I need to remove items selected when a button is clicked and update an item counter, but this operation doesn't work anymore.. items are not removed and counter not updated.

This is the code I tried to do:

Case $del
            GUICtrlSetState($go, $GUI_FOCUS)
            _GUICtrlListView_DeleteItemsSelected($hListView)
                
            _GUICtrlListView_GetItemSelected($hListView, 1)

    ; this is the old code
    ;While Not GUICtrlRead($hListView) = 0
    ;   GUICtrlDelete(GUICtrlRead($hListView))
    ;   _GUICtrlListView_SetItemSelected($hListViewH, GUICtrlRead($hListView))
    ;   GUICtrlDelete(GUICtrlRead($hListViewH))
        ; remove from the counter
    ;   $count = $count - 1
    ;   GUICtrlSetData($counter, $count)
    ;WEnd
@Authenticity it's one of the behind the scenes methods the devs use to make the controls more user friendly, so listview items become pseudo controls that are deletable with a universal GuiCtrlDelete() function - programming with training wheels if you like, but very convenient for fast coding.

@Lupo73

I'm assuming you want _GUICtrlListView_SetItemSelected() and not _GUICtrlListView_GetItemSelected() in your example?

unless your updating the counter somewhere else, it's in the commented out while loop.

are you using GUICtrlCreateListViewItem() to create items?

if not then you need to use a handle and not the control ID from GUICtrlCreateListView() _GUICtrlListView_DeleteItemsSelected(GUICtrlGetHandle($hListView))

you want index 0 for first item selected _GUICtrlListView_SetItemSelected($hListView, 0)

index 0 is selected but you have the Go button in focus so it will be greyed out.

post current version of your script with second hidden listview code removed.

you can use _GUICtrlListView_GetItemParam($hListview, $iIndex) to retrieve the control ID for each listview item

to use with GUICtrlDelete() if you only use GUICtrlCreateListViewItem() to add items.

you can keep track of items by using a multi dimension array with all your item data and the control id of the item it matches

just retrieve the control id for each item and compare to your array.

or

you can add your own tracking info (numeric data, array element number or struct pointer) to lparam element of LVITEM struct as you create new items with last parameter of _GUICtrlListView_AddItem()

$item = _GUICtrlListView_AddItem($hListView, $main, -1, 0); parameter 4 is lParam
_GUICtrlListView_AddSubItem($hListView, $item, $dir, 1)
_GUICtrlListView_AddSubItem($hListView, $item, ConvSize($size), 2)
_GUICtrlListView_AddSubItem($hListView, $item, " - ", 3)
;$item = GUICtrlCreateListViewItem($main & "|" & $dir & "|" & ConvSize($size) & "| - ", $hListView)

saves making an additional _GUICtrlListView_SetItemParam() call.

at any rate post your current script version and I'm certain someone if not myself will give you some ideas or an entire solution.

using the lParam element is the way to go if you want to track data for each item as Authenticity suggested, it's what I use in a listview UDF I wrote.

over to you.


I see fascists...

Share this post


Link to post
Share on other sites
Lupo73

Thanks, I have used your code (AddItem and AddSubItem functions) and now removing items with this function _GUICtrlListView_DeleteItemsSelected(GUICtrlGetHandle($hListView)) work correctly :)


SFTPEx, AutoCompleteInput_DateTimeStandard(), _ImageWriteResize()_GUIGraduallyHide(): some AutoIt functions.

Lupo PenSuite: all-in-one and completely free selection of portable programs and games.

DropIt: a personal assistant to automatically manage your files.

ArcThemALL!: application to multi-archive your files and folders.

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.