Page 1 of 1

Problems with ListView and ListView_FindItem

Posted: Wed Jan 05, 2005 5:20 pm
by fliptw
I have a ListView Control, with 3 columns. I want to able to press a key and have the first item that starts with the given letter selected. the problem is, ListView_FindItem is always returning zero. what I am doing wrong?

Code: Select all

case LVN_KEYDOWN:
{
  NMLVKEYDOWN* keyDown = (LPNMLVKEYDOWN) lParam;
  
  if(keyDown->wVKey >= 0x41 && keyDown->wVKey <= 0x5a)
  {
    LVFINDINFO lvfi;
    ZeroMemory(&lvfi, sizeof(LVFINDINFO));
    TCHAR searchTerm[2];
    
    sprintf(searchTerm,"%c",keyDown->wVKey);
    lvfi.flags = LVFI_PARTIAL|LVFI_STRING;
    lvfi.psz = searchTerm;
    int idx=ListView_FindItem(GetDlgItem(hDlg,IDC_ROMLIST), -1, &lvfi);
    ListView_SetSelectionMark(GetDlgItem(hDlg,IDC_ROMLIST), idx);
    ListView_SetItemState(GetDlgItem(hDlg,IDC_ROMLIST), idx, LVIS_SELECTED|LVIS_FOCUSED, LVIS_FOCUSED|LVIS_SELECTED);
    ListView_EnsureVisible(GetDlgItem(hDlg,IDC_ROMLIST), idx, FALSE);
  }
  break;
}

Posted: Thu Jan 06, 2005 4:38 am
by Nirvana
0 would be the first index... if the first index had the character pressed, it would find that one... are you sure it's not a user error?

Posted: Thu Jan 06, 2005 2:45 pm
by fliptw
my understanding of LVFI_PARTIAL is that it matches the start of the string, so If I press A, it'll match the first item that starts with A, and so on.

it returns 0 even if the letter doesn't exist in any of the items.


this listview control is displaying contents of a given folder.

Posted: Thu Jan 06, 2005 5:57 pm
by DCrazy
Try including LVFI_WRAP so that typing works from anywhere in the list.

Posted: Fri Jan 07, 2005 1:11 am
by fliptw
Sure, leave to MS not to tell you one needs to process the LVN_ODFINDITEM notification.

ListView can't search anything by itself.

Posted: Fri Jan 07, 2005 9:15 am
by DCrazy
:roll: It actually makes sense, because no common controls ever do anything on their own... it's always up to us to re-implement the same basic functionality.

Posted: Fri Jan 07, 2005 3:04 pm
by fliptw
the funny thing is, this listview worked properly a year ago.

no LVN_ODFINDITEM anywhere in the code.