how can I count the number of columns in a listview ?

I have tried this in a loop, but it crashes:

inc counter
mov lvi.iSubItem,counter
invoke SendMessage,control,LVM_GETITEM,0,addr lvi
cmp eax,FALSE
je end

Posted on 2003-01-26 07:52:16 by Sm?rebr?d
Try this:

First, get the handle to the header control..
invoke SendMessage, control, LVM_GETHEADER, 0, 0
mov, hHeader, eax

and this will return the handle to the listview header control in eax

Next do a sendmessage on the Header Control...
invoke SendMessage, hHeader, HDM_GETITEMCOUNT, 0, 0
and now eax will contain the count of listview columns, or -1 if an error...
Posted on 2003-01-26 08:45:18 by Gunner

Thank you Gunner, but that doesn't work.
I use LVM_GETITEMTEXT now and it works fine.

Posted on 2003-01-26 14:22:48 by Sm?rebr?d
I just wanna back up Gunner in case anyone searching for this solution happens to come upon this thread. His method does indeed work, and is also FAR more efficient than setting up a loop that sends a bunch of string based messages... =/
Posted on 2003-01-26 23:04:17 by BubbaFate
I wonder if Sm?rebr?d is blond.
Posted on 2003-01-27 09:24:56 by comrade

I'm not blond... ;-)

Gunners method seems not to work unter WindowsXP, it only returns -1.
And I have to read out ALL item texts, so I had to use a loop, ok ?

Posted on 2003-01-27 10:11:16 by Sm?rebr?d
im runnin XP, here is a snipplet of the code i used to test...


szFmt [COLOR=blue]db[/COLOR] [COLOR=orange]"%lu"[/COLOR][COLOR=gray],[/COLOR] [COLOR=red]0[/COLOR]
szBuffer [COLOR=blue]db[/COLOR] [COLOR=red]100[/COLOR] [COLOR=blue]dup[/COLOR][COLOR=gray](?)[/COLOR]

[COLOR=blue]invoke[/COLOR] SendMessage[COLOR=gray],[/COLOR] hwndLV[COLOR=gray],[/COLOR] LVM_GETHEADER[COLOR=gray],[/COLOR] [COLOR=red]0[/COLOR][COLOR=gray],[/COLOR] [COLOR=red]0[/COLOR]
[COLOR=blue]invoke[/COLOR] SendMessage[COLOR=gray],[/COLOR] [COLOR=magenta]eax[/COLOR][COLOR=gray],[/COLOR] HDM_GETITEMCOUNT[COLOR=gray],[/COLOR] [COLOR=red]0[/COLOR][COLOR=gray],[/COLOR] [COLOR=red]0[/COLOR]
[COLOR=blue]invoke[/COLOR] wsprintf[COLOR=gray],[/COLOR] [COLOR=blue]addr[/COLOR] szBuffer[COLOR=gray],[/COLOR] [COLOR=blue]addr[/COLOR] szFmt[COLOR=gray],[/COLOR] [COLOR=magenta]eax[/COLOR]
[COLOR=blue]invoke[/COLOR] MessageBox[COLOR=gray],[/COLOR] NULL[COLOR=gray],[/COLOR] [COLOR=blue]addr[/COLOR] szBuffer[COLOR=gray],[/COLOR] NULL[COLOR=gray],[/COLOR] MB_OK
Posted on 2003-01-27 10:31:40 by BubbaFate
The code I posted works on all windows platforms.
it is not working most likely because you are passing the wrong paramter or not passing the handle to your listview.

What is great about the API is that when they encounter an error, they use SetLastError (Most do anyway) which means you can use GetLastError.

Borrowing BubbaFates' code:

invoke SendMessage, hwndLV, LVM_GETHEADER, 0, 0
invoke SendMessage, eax, HDM_GETITEMCOUNT, 0, 0
invoke GetLastError
PrintDec eax

If GetLastError returns zero, that means there was no error. When a function returns -1 it is just telling you there was an error and you should use GetLastError to get the exact error code ;)
Posted on 2003-01-27 10:55:48 by Gunner
Your first question was how to get the listview column count...

Now you say you want to get all listitems text... When I first started with the listview and wanted to do what you are doing, I tried with a loop and it never worked.. Here is what I use:

mov ecx, -1
push LVNI_ALL ; Get next listview item
push ecx ; LVITEM index
push hListView2 ;
call SendMessage ;
test eax, eax ; have anymore items?
js NoMoreItems ; nope

mov ecx, eax

; Get listview items info
mov lvi.imask, LVIF_PARAM
mov lvi.iItem, ecx
mov lvi.iSubItem, 0
invoke SendMessage, hListView2, LVM_GETITEM, 0, addr lvi
; do stuff with the listitem info here Sm?rebr?d
jmp GetNextItem
Posted on 2003-01-27 11:09:18 by Gunner