Shoutbox

[?] Clearing LstView Controls - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: [?] Clearing LstView Controls (/showthread.php?tid=77946)

[?] Clearing LstView Controls by Spunky on 10-03-2007 at 02:07 PM

I have trouble with this before, but for some reason this doesn't seem to clear the whole list, it always seems to leave the last item if there are more than 2-3 items in the list... Any idea why? It looks Ok to me and I've tried looping it more times, but it doesn't work

code:
for(var i=0; i<=PlusWnd.LstView_GetCount("ListView");i++){
    PlusWnd.LstView_RemoveItem("ListView", 0);
}


RE: [?] Clearing LstView Controls by Matti on 10-03-2007 at 02:32 PM

Try this instead! ;)

code:
//ListView_DeleteAll - Deletes all items
//Parameters: hWnd = control handle
function ListView_DeleteAll(hWnd) {
   var LVM_FIRST = 0x1000;
   var LVM_DELETEALLITEMS = (LVM_FIRST + 9);

   return new Boolean(Interop.Call("user32", "SendMessageW", hWnd, LVM_DELETEALLITEMS, 0, 0));
}

//Example
ListView_DeleteAll(PlusWnd.GetControlHandle("ListView"));

RE: [?] Clearing LstView Controls by Volv on 10-03-2007 at 02:40 PM

Or you could use a while(PlusWnd.LstView_GetCount("ListView")) loop which would probably work as well. But I suppose Mattike's method is better :p

EDIT: Also your current method is looping 1 more time than is necessary as LstView_GetCount gives you the number of items in the listview, not the highest index - have you checked your Script Debugging Window?


RE: [?] Clearing LstView Controls by Spunky on 10-03-2007 at 02:49 PM

quote:
Originally posted by Mattike
Try this instead!

That causes an error and so doesn't work

quote:
Originally posted by Volv
Or you could use a while(PlusWnd.LstView_GetCount("ListView")) loop

That just isn't removing any items ^o)

quote:
Originally posted by Volv
EDIT: Also your current method is looping 1 more time than is necessary - have you checked your Script Debugging Window?

I was tracing the LstView_Count property and it never got down to zero... it would just keep looping at 1



EDIT: fixed a spelling mistake in Mattike's.... Now it does the same thing as Volv's... Lol.
RE: [?] Clearing LstView Controls by Volv on 10-03-2007 at 02:58 PM

I believe your original problem is related to the fact that PlusWnd.LstView_GetCount("ListView") is not constant (it decreases each time you remove an item) and as such your 'i' isn't going through all the necessary loops.
It works for 1 item because it loops through '0'.
It works for 2 items because it loops through '0', '1'.
It does not work for 3 items because it loops through '0', '1'.
It does not work for 4 items because it loops through '0', '1', '2'.
etc.

EDIT: I don't see why my while() loops wouldn't work though :P (I haven't tried it though)


RE: [?] Clearing LstView Controls by Spunky on 10-03-2007 at 03:00 PM

I did manage to do it once before...

@Volv: The loop always removes index 0 so it should always remove the top one in the list... there should ALWAYS be a top item in the list

I think we should have a function to clear LstViews and Combos in the next version ;)


RE: [?] Clearing LstView Controls by markee on 10-03-2007 at 03:03 PM

It should be

code:
while(PlusWnd.LstView_GetCount("ListView") != 0)
This will give a boolean expression like the while statement wants ;)

This method I have found works well, I have used it before with Message Customizer! Live when I did the groups to clear out the list views. However, seeing as though Mattike uses windows API it might be quicker and nicer to use that (especially on larger lists).
RE: [?] Clearing LstView Controls by Spunky on 10-03-2007 at 03:04 PM

quote:
Originally posted by markee
code:
while(PlusWnd.LstView_GetCount("ListView") != 0)


I did try that too... I might give it another go though
RE: RE: [?] Clearing LstView Controls by Volv on 10-03-2007 at 03:07 PM

quote:
Originally posted by markee
It should be
code:
while(PlusWnd.LstView_GetCount("ListView") != 0)
This will give a boolean expression like the while statement wants ;)

code:
while(PlusWnd.LstView_GetCount("ListView") > 0)
Hmph, > owns != :P



quote:
Originally posted by SpunkyLoveMuff
I did try that too... I might give it another go though
You should have something like this:
code:
while(PlusWnd.LstView_GetCount("ListView") > 0){
    PlusWnd.LstView_RemoveItem("ListView", 0);
}

RE: [?] Clearing LstView Controls by markee on 10-03-2007 at 03:14 PM

It doesn't matter if you use > or != in this situation because there is no time when it won't return a number equal to or greater than zero.  This is because the only error is if the plus window does not exist or the control ID doesn't exist.  As far as I can tell wouldn't it be quickest to do !== rather than the other 2?


RE: RE: [?] Clearing LstView Controls by Matti on 10-03-2007 at 05:32 PM

quote:
Originally posted by SpunkyLoveMuff
quote:
Originally posted by Mattike
Try this instead!

That causes an error and so doesn't work
Uh? Then why does it work flawlessly here? Can you tell me the error please?
RE: [?] Clearing LstView Controls by Spunky on 10-03-2007 at 05:45 PM

quote:
Originally posted by Mattike
Uh? Then why does it work flawlessly here? Can you tell me the error please?

Object Expected. Spelling mistake in your post... I did edit mine to tell you. It didn't seem to be working anyway, but then again neither did Volv's and that is working now. I think my PC is playing up and I have reset since I last posted.
RE: [?] Clearing LstView Controls by roflmao456 on 10-03-2007 at 07:47 PM

yo.

code:
PlusWnd.SendControlMessage("controlID", 0x1009 /* LVM_DELETEALLITEMS */, 0, 0);


RE: [?] Clearing LstView Controls by phalanxii on 10-04-2007 at 12:47 AM

I would lean more towards using Mattike/roflmao456's method of using the LVM_DELETEALLITEMS message.

In my previous experience, doing a loop to remove each item is considerably slower and you can see the list getting cleared one by one.

Using the LVM_DELETEALLITEMS message clears the list altogether, and not only does that look nicer, but it probably uses less memory (considering only 1 line is called compared to the n lines used in a loop).

Just my two cents.


RE: [?] Clearing LstView Controls by Matti on 10-04-2007 at 04:56 PM

Yes indeed, the LVM_DELETEALLITEMS is the best, most efficient and fastest way you can get: it's the pure Win32 API! (and we all love that thing don't we?)

I suggest you to try fix my code (make use of that Debug::Trace function!) or try roflmao456's code. The reason why my function uses a call to SendMessage instead of using PlusWnd::SendControlMessage is so that I don't have to pass that whole PlusWnd object to the function, but roflmao's code will do just the same thing and may turn out even more interesting if you only want to use the list cleaning one or two times and you don't really need a separate function for it. :)