Strange List View Delete Behavior LV_Delete() [ solved ]

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Strange List View Delete Behavior LV_Delete() [ solved ]

20 Feb 2014, 21:35

Not sure if this is a bug, or something I've overlooked:

Code: Select all

Gui, Add, ListView, Checked -LV0x10, a|b
Gui, Add, Button, Section gClear, Clear Checked
Gui, Add, Button, ys gClearDebug, Clear [ debug ]
Gui, Add, Button, ys gPopulate, Re-Populate
Gui, Show

Populate:
LV_Delete()
Loop 10
    LV_Add("check", "", "some val " a_index )
return

Clear:
    ClearItems()
Return

ClearDebug:
    ClearItems( 1 )
Return

GuiClose:
ExitApp

ClearItems( deb = "" )
{
    Loop % LV_GetCount()
    {
        if ( LV_GetNext( a_index-1, "Checked" ) = a_index )
        {
            if ( !deb )
                LV_Delete( a_index )
            else
                msgbox % "Remove Item" a_index
        }
    }

    msgbox % ( !deb ? LV_GetCount() " items remain." : "complete!" )
}
Its suppose to remove checked ListView Items
but for some reason, it does not remove them all
I've tried native AHk + Win_32 API Calls and many other configs,
all end up with the same incorrect results.

I also added a debugger button so you can see what `should` happen.

Any tests/results would be greatly appreciated,

Thanks
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Re: Strange List View Delete Behavior

20 Feb 2014, 22:25

After bouncing ideas around with my irc brethren,
I came up with a workaround ( if I was even correct to begin with ).

Code: Select all

Gui, Add, ListView, Checked -LV0x10, a|b
Gui, Add, Button, Section gClear, Clear Checked
Gui, Add, Button, ys gPopulate, Re-Populate
Gui, Show

Populate:
LV_Delete()
Loop 10
    LV_Add("check", "", "some val " a_index )
return

Clear:
Loop % ( LV_GetCount(), ClearArray := [] )
{
    if ( LV_GetNext( a_index-1, "Checked" ) = a_index )
    {
        LV_GetText( val, a_index, 2 )
        ClearArray.Insert( val )
    }
}

For i in ( ClearArray, LV_Delete() )
{
    LV_Add("check", "", ClearArray[ i ] )
}
Return
It seems to work better than a conditional delete item,
as the LV itself is updated ( lv resized, scroll-bars removed where applicable ).

If there is a technical explanation as to why this was happening in the 1st place,
I'd be all ears.


Thanks again..
hd0202
Posts: 183
Joined: 04 Oct 2013, 03:07
Location: Germany near Cologne

Re: Strange List View Delete Behavior

21 Feb 2014, 01:28

TLM wrote:If there is a technical explanation as to why this was happening in the 1st place,
I'd be all ears.
After you delete line1 -> line2 becomes line1, line3 -> line2
if you now delete line2 you actually delete line3 and so on...

solution: multi deletes must be done bottom up!

Hubert
User avatar
Sjc1000
Posts: 39
Joined: 02 Oct 2013, 02:07

Re: Strange List View Delete Behavior LV_Delete() [ solved ]

21 Feb 2014, 04:57

Ahh i see you got it sorted. Good to hear.. No array setup then :P
Please find me on the IRC if you have any questions, I'm never on the forum anymore.
hunter99
Posts: 129
Joined: 20 Jan 2014, 17:57

Re: Strange List View Delete Behavior LV_Delete() [ solved ]

21 Feb 2014, 17:46

Hi TLM, here is a way without the array. Commented out the ClearDebug and removed the ref. to deb.
Left in a couple of msgboxs, which can be removed. Got most of the code right from the docs.
Had a lot of fun playing with it so I though I would pass it on.

hunter

Code: Select all

totalrows = 10
Gui, Add, ListView, Checked -LV0x10, a|b
Gui, Add, Button, Section gClear, Clear Checked
;Gui, Add, Button, ys gClearDebug, Clear [ debug ]
Gui, Add, Button, ys gPopulate, Re-Populate
Gui, Show
return

Populate:
LV_Delete()
Loop %totalrows%
    LV_Add("check", "", "some val " a_index )
MsgBox, 0, , # OF ROWS = %totalrows%
return

;ClearDebug:    ;not sure what this was for, as well as "deb" which I removed .
 ;   ClearItems( 1 )
;Return

GuiClose:
ExitApp


Clear:
     RowNumber = 0
    Loop
       { 
        RowNumber := LV_GetNext(rownumber, "Checked")  ; Resume the search at the row after that found by the previous iteration
        if not RowNumber  ; The above returned zero, so there are no more selected rows.
        break
            LV_Delete(rownumber)
            rownumber = rownumber-1
                msgbox % "Remove Item" a_index
        }
    ExitApp
Morpheus
Posts: 119
Joined: 04 Oct 2013, 05:09

Re: Strange List View Delete Behavior LV_Delete() [ solved ]

21 Feb 2014, 19:42

I believe that using

Code: Select all

GuiControl, -Redraw, ListViewVar
before deleting, and

Code: Select all

GuiControl, +Redraw, ListViewVar
after solves the problem as well.
hd0202
Posts: 183
Joined: 04 Oct 2013, 03:07
Location: Germany near Cologne

Re: Strange List View Delete Behavior LV_Delete() [ solved ]

22 Feb 2014, 00:41

@Morpheus
Sorry, no, this supresses only the redrawing.

Hubert
Morpheus
Posts: 119
Joined: 04 Oct 2013, 05:09

Re: Strange List View Delete Behavior LV_Delete() [ solved ]

22 Feb 2014, 07:53

Sorry for the incorrect info Hubert. I looked more closely at the script I was referencing, and I did use a method that deleted from the bottom up. I must have used the -redraw for speed. :oops:
User avatar
AfterLemon
Posts: 85
Joined: 30 Sep 2013, 14:27
Location: Ohio, USA

Re: Strange List View Delete Behavior LV_Delete() [ solved ]

25 Feb 2014, 16:21

Both your own solution and hunter99's take from different ideas. One being array handling and post-processing deletion, and the other being top-down deletion.
Both of these methods obviously work, however to answer your original query in the style you were expecting...:
The issue you're seeing is that A_Index is not reliable when one must "skip" the un-checked items while still monitoring the shift of the higher value rows downward.

A solution is rather simple, though, using 1 variable:

Code: Select all

Gui, Add, ListView, Checked -LV0x10, a|b
Gui, Add, Button, Section gClear, Clear Checked
Gui, Add, Button, ys gClearDebug, Clear [ debug ]
Gui, Add, Button, ys gPopulate, Re-Populate
Gui, Show

Populate:
LV_Delete()
Loop 10
    LV_Add("check", "", "some val " a_index )
return

Clear:
    ClearItems()
Return

ClearDebug:
    ClearItems( 1 )
Return

GuiClose:
ExitApp

ClearItems( deb = "" )
{
	Loop % (LV_GetCount(),a:=1)
	{	if ( LV_GetNext( 0, "Checked" ) = a )
		{	if ( !deb )
				LV_Delete( a )
			else
				msgbox % "Remove Item" A_Index
		}else ++a
	}msgbox % ( !deb ? LV_GetCount() " items remain." : "complete!" )
}
HOME: Windows 11 Pro | AMD Ryzen 7 5800X 8-Core @ 4.50GHZ | 64GB RAM
MOBILE: Samsung Galaxy Note 10+
hunter99
Posts: 129
Joined: 20 Jan 2014, 17:57

Re: Strange List View Delete Behavior LV_Delete() [ solved ]

25 Feb 2014, 19:44

Hi AfterL , very nice. Am 75 and and love this stuff as it helps to keep the brain working. Thanks for posting that.
hunter
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Re: Strange List View Delete Behavior LV_Delete() [ solved ]

26 Feb 2014, 01:23

hunter99 wrote:Hi AfterL , very nice. Am 75
are you serious? that is totesawes!!!
would <3 to be writing code/scripts when I'm that age..


@AfterLemon, very nice work indeed.
ty again all

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 137 guests