For Programmers: Free Programming Magazines  


Home > Archive > VC Language > November 2005 > problem iterating over list view items









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author problem iterating over list view items
Steve

2005-11-28, 7:07 pm

Using this code:
<code>
LVITEM item;
item.mask = /*LVIF_STATE|*/LVIF_TEXT;
HWND hList = GetDlgItem(hDlg, IDC_LIST_CHARACTERS);
int n = ListView_GetItemCount(hList);
while(ListView_GetItem(hList, &item) == TRUE)
{
if(item.state&LVIS_SELECTED)
{
// this item is selected
int y = 5;
}
}
</code>

I am trying to determine if an item is selected. Before I can even get that
far, I need to get the code that loops through the items to work.
my call(debugging) to ListView_GetItemCount() return 2
I am inserting/setting the items with the LVIF_TEXT flag.

ListView_GetItem() macro returns FALSE. I've set the correct mask
(LVIF_TEXT) so I don't understand why it's not getting valid results?

Anyone have any hints for me?


Steve

2005-11-28, 7:07 pm

OK, I see the initial problem, I need to get an index first, then set that
index in the LVITEM. I have done that and am correctly getting the index of
the selected item, however I still can't get the item data

I am not using this code:
<code>
LVITEM item;
item.mask = /*LVIF_STATE|*/LVIF_TEXT;

HWND hList = GetDlgItem(hDlg, IDC_LIST_CHARACTERS);

int index = -1;
while(TRUE)
{
index = ListView_GetNextItem(hList, index, LVNI_SELECTED);
if(index > -1)
{
item.iItem = index;
item.iSubItem = 1;
if(ListView_GetItem(hList, &item) == TRUE)
{
// this is a selected item
int y = 5;
}
}
else
{
break;
}
}
</code>

my breakpoint on the 'int y = 5' line never get's hit. I have tried
different sub item indices, still no change...



"Steve" <sss@sss.com> wrote in message
news:uqhSp1F9FHA.2040@TK2MSFTNGP14.phx.gbl...
> Using this code:
> <code>
> LVITEM item;
> item.mask = /*LVIF_STATE|*/LVIF_TEXT;
> HWND hList = GetDlgItem(hDlg, IDC_LIST_CHARACTERS);
> int n = ListView_GetItemCount(hList);
> while(ListView_GetItem(hList, &item) == TRUE)
> {
> if(item.state&LVIS_SELECTED)
> {
> // this item is selected
> int y = 5;
> }
> }
> </code>
>
> I am trying to determine if an item is selected. Before I can even get

that
> far, I need to get the code that loops through the items to work.
> my call(debugging) to ListView_GetItemCount() return 2
> I am inserting/setting the items with the LVIF_TEXT flag.
>
> ListView_GetItem() macro returns FALSE. I've set the correct mask
> (LVIF_TEXT) so I don't understand why it's not getting valid results?
>
> Anyone have any hints for me?
>
>



Igor Tandetnik

2005-11-28, 7:07 pm

Steve <sss@sss.com> wrote:
> Using this code:
> <code>
> LVITEM item;
> item.mask = /*LVIF_STATE|*/LVIF_TEXT;
> HWND hList = GetDlgItem(hDlg, IDC_LIST_CHARACTERS);
> int n = ListView_GetItemCount(hList);
> while(ListView_GetItem(hList, &item) == TRUE)
> {
> if(item.state&LVIS_SELECTED)
> {
> // this item is selected
> int y = 5;
> }
> }
> </code>
>
> I am trying to determine if an item is selected. Before I can even
> get that far, I need to get the code that loops through the items to
> work.
> my call(debugging) to ListView_GetItemCount() return 2
> I am inserting/setting the items with the LVIF_TEXT flag.
>
> ListView_GetItem() macro returns FALSE. I've set the correct mask
> (LVIF_TEXT) so I don't understand why it's not getting valid results?


You need to set LVITEM::iItem to the index of the item you are trying to
retrieve, and iSubItem to zero (unless you actually want to retrieve a
subitem). Further, since you are requesting the text of the item, you
need to have pszText point to a buffer and cchTextMax specify the size
of this buffer in characters. Further still, since you have not
requested LVIF_STATE, you have no business checking item.state field -
it was not retrieved and contains garbage.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


Steve

2005-11-28, 7:07 pm

OK, this is working now. I need to spend more time on MSDN, once I found
the correct pages, a quick read made it all clear.
Thanks!


"Steve" <sss@sss.com> wrote in message
news:uyu$O9F9FHA.2036@TK2MSFTNGP14.phx.gbl...
> OK, I see the initial problem, I need to get an index first, then set that
> index in the LVITEM. I have done that and am correctly getting the index

of
> the selected item, however I still can't get the item data
>
> I am not using this code:
> <code>
> LVITEM item;
> item.mask = /*LVIF_STATE|*/LVIF_TEXT;
>
> HWND hList = GetDlgItem(hDlg, IDC_LIST_CHARACTERS);
>
> int index = -1;
> while(TRUE)
> {
> index = ListView_GetNextItem(hList, index, LVNI_SELECTED);
> if(index > -1)
> {
> item.iItem = index;
> item.iSubItem = 1;
> if(ListView_GetItem(hList, &item) == TRUE)
> {
> // this is a selected item
> int y = 5;
> }
> }
> else
> {
> break;
> }
> }
> </code>
>
> my breakpoint on the 'int y = 5' line never get's hit. I have tried
> different sub item indices, still no change...
>
>
>
> "Steve" <sss@sss.com> wrote in message
> news:uqhSp1F9FHA.2040@TK2MSFTNGP14.phx.gbl...
> that
>
>



Heinz Ozwirk

2005-11-28, 7:07 pm

"Steve" <sss@sss.com> schrieb im Newsbeitrag
news:uyu$O9F9FHA.2036@TK2MSFTNGP14.phx.gbl...
> OK, I see the initial problem, I need to get an index first, then set that
> index in the LVITEM. I have done that and am correctly getting the index
> of
> the selected item, however I still can't get the item data
>
> I am not using this code:
> <code>
> LVITEM item;
> item.mask = /*LVIF_STATE|*/LVIF_TEXT;
>
> HWND hList = GetDlgItem(hDlg, IDC_LIST_CHARACTERS);
>
> int index = -1;
> while(TRUE)
> {
> index = ListView_GetNextItem(hList, index, LVNI_SELECTED);
> if(index > -1)
> {
> item.iItem = index;
> item.iSubItem = 1;


Are you sure you want a sub-item? Or do you actually want the text of the
item itself? Then you should initialize iSubItem with 0. Also, you did not
specify a buffer for the text. Add something like

TCHAR text[42];
item.pszText = &text[0];
item.cchTextMax = sizeof(text) / sizeof(text[0]);

> if(ListView_GetItem(hList, &item) == TRUE)


Don't compare with TRUE. Many functions returning a BOOL do return other
non-zero values for success, even if the docs say, they will return "TRUE if
successfull, or FALSE otherwise". Just to make sure, you should never
compare with TRUE (or true) but always with FALSE (or false), or don't
compare BOOL (or bool), but use the boolean value itself as a condition.
Either write

if (ListView_GetItem(hList, &item) != FALSE)

or simply.

if (ListView_GetItem(hList, &item))

I do not suggest, however, to replace all tests for zero/non-zero in that
way. Only use them if the expression only returns (various possible values
for) success or failure.

> {
> // this is a selected item
> int y = 5;
> }
> }
> else
> {
> break;
> }
> }
> </code>
>
> my breakpoint on the 'int y = 5' line never get's hit. I have tried
> different sub item indices, still no change...


HTH
Heinz


Steve

2005-11-28, 7:07 pm


"Igor Tandetnik" <itandetnik@mvps.org> wrote in message
news:OuDlY9F9FHA.2036@TK2MSFTNGP14.phx.gbl...
> Steve <sss@sss.com> wrote:
>
> You need to set LVITEM::iItem to the index of the item you are trying to
> retrieve, and iSubItem to zero (unless you actually want to retrieve a
> subitem). Further, since you are requesting the text of the item, you
> need to have pszText point to a buffer and cchTextMax specify the size
> of this buffer in characters. Further still, since you have not
> requested LVIF_STATE, you have no business checking item.state field -
> it was not retrieved and contains garbage.



I see that now, thank you Igor, your help is always appreciated very much.
-Steve


Steve

2005-11-28, 7:07 pm


"Heinz Ozwirk" <hozwirk.SPAM@arcor.de> wrote in message
news:438b783e$0$20859$9b4e6d93@newsread2
.arcor-online.net...
> "Steve" <sss@sss.com> schrieb im Newsbeitrag
> news:uyu$O9F9FHA.2036@TK2MSFTNGP14.phx.gbl...
that[color=darkred]
index[color=darkred]
>
> Are you sure you want a sub-item? Or do you actually want the text of the
> item itself? Then you should initialize iSubItem with 0. Also, you did not
> specify a buffer for the text. Add something like
>
> TCHAR text[42];
> item.pszText = &text[0];
> item.cchTextMax = sizeof(text) / sizeof(text[0]);
>
>
> Don't compare with TRUE. Many functions returning a BOOL do return other
> non-zero values for success, even if the docs say, they will return "TRUE

if
> successfull, or FALSE otherwise". Just to make sure, you should never
> compare with TRUE (or true) but always with FALSE (or false), or don't
> compare BOOL (or bool), but use the boolean value itself as a condition.
> Either write
>
> if (ListView_GetItem(hList, &item) != FALSE)
>
> or simply.
>
> if (ListView_GetItem(hList, &item))
>
> I do not suggest, however, to replace all tests for zero/non-zero in that
> way. Only use them if the expression only returns (various possible values
> for) success or failure.
>
>
> HTH
> Heinz



Thank you Heinz, that is a very thorough response, I appreciate it!


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com