Home > Software design >  ListView_GetNextItem always returning 0
ListView_GetNextItem always returning 0

Time:08-12

I have some code that currently causes an infinite loop and I'm unable to find an reason as to why.

The code is designed to set bit-flags on an integer based on items that are selected in the listbox.This is handled by the case statement within the While loop.

I've followed the code through on a debugger and the value of selectedItem never changes. The handle to the listbox appears to be valid and is populated using the same handle.

I've tried using both the SendMessage function and ListView_GetNextItem macro, former is commented out in my code.

Any help would be appreciated, I'm assuming I'm missing something obvious here!

Edit: I was basing this loop off the one seen here: win32 retrieve index of all selected items from listview

int getTypeStatus()
{
    int retVal =0;
    //int selectedItem = SendMessage(lstFileStatus, LVM_GETNEXTITEM, (WPARAM)-1, MAKELPARAM(LVIS_SELECTED,0));
    int selectedItem = ListView_GetNextItem(lstFileStatus,-1, LVNI_SELECTED);
    while (selectedItem != -1)
    {
        switch (selectedItem){
        case 0:
            retVal = retVal | NOT_VERIFIED;
            break;
        case 1:
            retVal = retVal | IRRELEVANT;
            break;
        case 2:
            retVal = retVal | NOT_IN_LIST;
            break;
        case 3:
            retVal = retVal | CONFIRMED;
            break;
        case 4:
            retVal = retVal | NOT_CONFIRMED;
            break;
        case 5:
            retVal = retVal | NEWLY_IDENTIFIED;
            break;
        case 6:
            retVal = retVal | MISMATCH_DETECTED;
            break;
        }
        selectedItem = ListView_GetNextItem(lstFileStatus,selectedItem, LVNI_SELECTED);
    }
    return retVal;
}

Edit: Also included code for creating control and populating (which works)

lstFileStatus = CreateWindowEx(0,"ListBox","",WS_CHILD|WS_VISIBLE|LBS_NOTIFY|WS_BORDER|LBS_EXTENDEDSEL,LeftHandStartX,TypeLineY,130,170,hwnd,(HMENU)IDC_LBX_TYPESTATUS,GetModuleHandle(NULL),0);
if (!lstFileStatus) {outputControlOutputError("lstFileStatus");}
for (int i=0;i<numTypeStatus;i  )
{
    SendMessage(lstFileStatus,LB_ADDSTRING,0,(LPARAM)arrayTypeStatus[i]);
}

CodePudding user response:

LVM_GETNEXTITEM is listview control message, but your control is a listbox. They're different controls and the messages aren't interchangable.

To get the selected items from a multi-select listbox you need to use LB_GETSELCOUNT to get the number of selections, allocate an array of ints that size, and then use LB_GETSELITEMS to get the selection indices.

CodePudding user response:

According to documentation definition of macro ListView_GetNextItem is as a:

void ListView_GetNextItem(HWND hwnd, int i, UINT flag)

so you are not supposed to get anything as a returned value. You can check official documentation yourself.

  • Related