Is there a message or function I can use to get the number of columns in a list view control or am I on my own with this one?
Posted on 2004-09-06 07:15:02 by Odyssey
As far as i know, explicitly no.

But use LVM_GETCOLUMN in a loop until FALSE is returned, then use the last value that was entered into iCol and increase it by 1 --- but i suppose someone will be more knowledgefull than i am on this subject.

Black iCE
Posted on 2004-09-06 08:23:25 by Black iCE

But use LVM_GETCOLUMN in a loop until FALSE is returned, then use the last value that was entered into iCol and increase it by 1 --- but i suppose someone will be more knowledgefull than i am on this subject


This is what I did because I haven't seen anything to help me in the sdk reference.
Posted on 2004-09-06 08:33:18 by Odyssey
from msdn:


An application sends an LB_GETCOUNT message to retrieve the number of items in a list box.

Syntax


To send this message, call the SendMessage function as follows.
lResult = SendMessage( // returns LRESULT in lResult (HWND) hWndControl, // handle to destination control (UINT) LB_GETCOUNT, // message ID (WPARAM) wParam, // = (WPARAM) () wParam; (LPARAM) lParam // = (LPARAM) () lParam; );
Parameters

wParam
Not used; must be zero.
lParam
Not used; must be zero.
Return Value

The return value is the number of items in the list box, or LB_ERR if an error occurs.


:wink: Dominik
Posted on 2004-09-06 08:51:04 by Dom
I am using a list view control not listbox. :)
Posted on 2004-09-06 08:55:27 by Odyssey
second msdn quote:

LVM_GETITEMCOUNT Message

--------------------------------------------------------------------------------

Retrieves the number of items in a list-view control. You can send this message explicitly or by using the ListView_GetItemCount macro.

Syntax


To send this message, call the SendMessage function as follows.
lResult = SendMessage( // returns LRESULT in lResult (HWND) hWndControl, // handle to destination control (UINT) LVM_GETITEMCOUNT, // message ID (WPARAM) wParam, // = 0; not used, must be zero (LPARAM) lParam // = 0; not used, must be zero );
Parameters

wParam
Must be zero.
lParam
Must be zero.


Dom
Posted on 2004-09-06 09:01:36 by Dom
LVM_GETITEMCOUNT returns the number of ROWS, not COLUMNS.

Enumerating the columns via LVM_GETCOLUMN is workable but silly, since we SHOULD know how many columns are in the control (assuming we in fact created it). If you create the control, keep track of the column count yourself. If you are enumerating a foreign control, use the GETCOLUMN method.
Posted on 2004-09-06 09:05:59 by Homer
Odyssey,

I am assuming that you are not the one creating the colums, else you would have been able to tell how many there are in your listview--- right?

The only way that i know when you (programmer) will be unawaire about the number of colums would be when he is doing Shell Programing and using IShellDetails interface.

So just out of curiosity, Shell right?

Black iCE
Posted on 2004-09-06 09:06:50 by Black iCE
I am the one creating the columns. The problem is at different points in the program there'll be a different number of colums and I think that the LV_COLUMN solution is simpler and/or cleaner. At any one time there won't be more than 10 columns so I don't believe there would be a great performance loss for using the LV_COLUMN method. However I stand to be corrected. :)
Posted on 2004-09-06 09:35:50 by Odyssey
How about using the LVM_GETHEADER message to get a handle to the header control of the list view and then sending the header control HDM_GETITEMCOUNT?
Posted on 2004-09-06 14:25:36 by Mecurius
Mecurius,

Thanks for the tip. I've tried it and it works. :)
Posted on 2004-09-06 15:56:31 by Odyssey
Mecurius suggestion VS others
Mecurius 1: Others 0.

Nice to know there are some real (men) problem solvers here :-D .
Posted on 2004-09-07 08:50:51 by Black iCE
Mecurius,

Thanks for the tip. I've tried it and it works. :)


For visible columns, this will work. For invisible - not.
Posted on 2004-09-08 02:35:02 by Morris
Since you ARE creating/deleting columns, why not just use a boring old global data variable to keep the CurrentColumns count?
This is a silly thread. End of rant.
Posted on 2004-09-09 09:40:20 by Homer
EvilHomer2k is right. Just create a variable and whenever creating/deleting a column increment/decrement it. That's all!



/siddhartha
Posted on 2004-09-09 09:43:47 by siddhartha

Since you ARE creating/deleting columns, why not just use a boring old global data variable to keep the CurrentColumns count?


Because it's boring :-D Seriously though I could use a global variable yes but unless I would get a significant performace increase from using it I don't see the point. Usually, there isn't a single way to do something in programming. Of course some solutions are better than others and if you can point out to me why using the global variable is better than I would probably go ahead and change my code otherwise I'm happy with my current solution and I don't see the point in changing it.
Posted on 2004-09-09 13:28:22 by Odyssey
Because it has no real cost in code (it makes your enumeration code redundant), because it costs just 1,2 or 4 bytes in binary data terms, because it is the simple and most efficient solution which is applicable - but hey, suit yourself, I'm just crying for the obvious to be recognized.
There's always more than one way to do anything in programming terms, but what's the golden directive of asmcoders? The golden directive is : by whatever means necessary, as long as the output is correct.
I'm a lateral thinker, don't feel that I am simply pushing the most obvious solution, it also happens to be (at least in this case) the cheapest.
If I am anything, I am obvious and cheap :)
Posted on 2004-09-11 08:57:10 by Homer