Item search in Listview control
Hi,In my win32 application i have a list view control with grid view style ,i want to search items/SubItem on this List view control.I used ListView_FindItem macro,But it always returning 0 for both item as well as subitem.How to find out an item or subitem string from a list view control
Code:
UINT uFindReplaceMsg;
BOOL CALLBACK SymbolNCommntDlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HWND HdlgFindOrReplce = NULL; // handle to Find dialog box
switch (message)
{
case WM_INITDIALOG:
{
ListView_SetExtendedListViewStyle(hWndListViewSymbol,LVS_EX_GRIDLINES| LVS_EX_FULLROWSELECT );
}
break;
case WM_NOTIFY:
{
switch(LOWORD(wParam))
{
case IDC_LIST1:
{
switch(((LPNMHDR) lParam)->code)
{
case LVN_KEYDOWN :
{
NMLVKEYDOWN * pnkd = (LPNMLVKEYDOWN) lParam;
//CNTRL+F Find Dialog Box created
if(pnkd->wVKey=='F' || pnkd->wVKey=='f')
{
if(GetKeyState(0x11)<0)
{
static FINDREPLACE Find; // common dialog box structure
static WCHAR szFindWhat[100]; // buffer receiving string
// Initialize FINDREPLACE ZeroMemory(&Find, sizeof(Find));
Find.lStructSize = sizeof(Find);
Find.hwndOwner = hWnd;
Find.lpstrFindWhat = szFindWhat;
Find.wFindWhatLen = _countof(szFindWhat);
Find.Flags =FR_HIDEUPDOWN|FR_HIDEMATCHCASE|FR_HIDEWHOLEWORD ;
HdlgFindOrReplce = FindText(&Find);
}
}
}
break;
default:
{
LPFINDREPLACE lpfr;
if (message == uFindReplaceMsg)
{
if (lpfr->Flags & FR_DIALOGTERM)
{
HdlgFindOrReplce = NULL;
return 0;
}
//If FindNext clicked searcching for the item in list view and make //it selected
if ((lpfr->Flags & FR_FINDNEXT))
{
int i;
LVFINDINFO plvfi={0};
plvfi.flags=LVFI_STRING;
plvfi.psz=lpfr->lpstrFindWhat;
i=ListView_FindItem(hWnd,-1,&plvfi);//returning 0 for any item
DWORD dw=GetLastError();//Returning 0
ListView_SetItemState(hWnd,i,LVIS_FOCUSED|LVIS_SELECTED,0x000F);//For selcting that item
}
else
return DefFrameProc(hWnd, hWndListViewSymbol,message, wParam, lParam);
}
}
}
}
break;
}
}
break;
default:
return FALSE;
}
return TRUE;
}
Re: Item search in Listview control
Quote:
Originally Posted by
manjut19
Hi,In my win32 application i have a list view control with grid view style ,i want to search items/SubItem on this List view control.I used ListView_FindItem macro,But it always returning 0 for both item as well as subitem.How to find out an item or subitem string from a list view control
- ListView_FindItem cannot search for sub-items, only for items.
- ListView_FindItem returns the index of the item if successful, or -1 otherwise. So zero means the very first item was found.
Re: Item search in Listview control
Code:
i=ListView_FindItem(hWnd,-1,&plvfi);//returning 0 for any item
DWORD dw=GetLastError();//Returning 0
If ListView_FindItem fails, it returns -1. Why are you checking GetLastError() ?
Code:
LPFINDREPLACE lpfr;
if (message == uFindReplaceMsg)
{
if (lpfr->Flags & FR_DIALOGTERM)
{
HdlgFindOrReplce = NULL;
return 0;
}
//If FindNext clicked searcching for the item in list view and make //it selected
if ((lpfr->Flags & FR_FINDNEXT))
{
int i;
LVFINDINFO plvfi={0};
plvfi.flags=LVFI_STRING;
plvfi.psz=lpfr->lpstrFindWhat;
i=ListView_FindItem(hWnd,-1,&plvfi);//returning 0 for any item
DWORD dw=GetLastError();//Returning 0
ListView_SetItemState(hWnd,i,LVIS_FOCUSED|LVIS_SELECTED,0x000F);//For selcting that item
}
else
Within the code block you are defining the variable lpfr. Where are you setting its contents before using it?:confused:
Re: Item search in Listview control
Another way to approach this is to use a virtual list backed by a std::list collection. The virtual list uses the collection to dyamically populate the items and subitems. You would then search the collection for the desired match, not the list control (which would effectively let you sesrch the data that makes up the subitems).
Re: Item search in Listview control
Quote:
Originally Posted by
2kaud
Code:
i=ListView_FindItem(hWnd,-1,&plvfi);//returning 0 for any item
DWORD dw=GetLastError();//Returning 0
If ListView_FindItem fails, it returns -1. Why are you checking GetLastError() ?
Within the code block you are defining the variable lpfr. Where are you setting its contents before using it?:confused:
Code:
LPFINDREPLACE lpfr;
if (message == uFindReplaceMsg)
{
lpfr=(LPFINDREPLACE)lParam; if (lpfr->Flags & FR_DIALOGTERM)
{
HdlgFindOrReplce = NULL;
return 0;
}
//If FindNext clicked searcching for the item in list view and make //it selected
if ((lpfr->Flags & FR_FINDNEXT))
{
int i;
LVFINDINFO plvfi={0};
plvfi.flags=LVFI_STRING;
plvfi.psz=lpfr->lpstrFindWhat;
i=ListView_FindItem(hWnd,-1,&plvfi);//returning 0 for any item
DWORD dw=GetLastError();//Returning 0
ListView_SetItemState(hWnd,i,LVIS_FOCUSED|LVIS_SELECTED,0x000F);//For selcting that item
}
else
Eventhough i am searching for second or third item
ListView_FindItem() returning zero only.
Re: Item search in Listview control
Quote:
Originally Posted by
manjut19
Code:
{
int i;
LVFINDINFO plvfi={0};
plvfi.flags=LVFI_STRING;
plvfi.psz=lpfr->lpstrFindWhat;
i=ListView_FindItem(hWnd,-1,&plvfi);//returning 0 for any item
DWORD dw=GetLastError();//Returning 0
ListView_SetItemState(hWnd,i,LVIS_FOCUSED|LVIS_SELECTED,0x000F);//For selcting that item
}
else
Eventhough
i am searching for second or third item
ListView_FindItem() returning zero only.
Did you debug your code?
What does your list control contain in its first three items?
What text are you searching for?
1 Attachment(s)
Re: Item search in Listview control
Quote:
Originally Posted by
VictorN
Did you debug your code?
What does your list control contain in its first three items?
What text are you searching for?
Ya i debugged the code,Attachment 32099
Suppose in Find text box i am giving a value 3 means lpfr->lpstrFindWhat
holding a value 3,but ListView_FindItem() returning zero only,it supposedto return 2 right??
Re: Item search in Listview control
Quote:
Originally Posted by
manjut19
Ya i debugged the code...
Suppose in Find text box i am giving a value 3 means lpfr->lpstrFindWhat
holding a value 3,but ListView_FindItem() returning zero only,it supposedto return 2 right??
Does it "mean lpfr->lpstrFindWhat holding a value 3"
or you were seeing in a watch window while debugging that "lpfr->lpstrFindWhat holding a value 3"?
Re: Item search in Listview control
Quote:
Originally Posted by
VictorN
Does it "mean lpfr->lpstrFindWhat holding a value 3"
or you were seeing in a watch window while debugging that "lpfr->lpstrFindWhat holding a value 3"?
In a watch window while debugging that "lpfr->lpstrFindWhat holding a value 3
Re: Item search in Listview control
Quote:
Originally Posted by
manjut19
Code:
UINT uFindReplaceMsg;
BOOL CALLBACK SymbolNCommntDlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HWND HdlgFindOrReplce = NULL; // handle to Find dialog box
switch (message)
{
case WM_INITDIALOG:
{
ListView_SetExtendedListViewStyle(hWndListViewSymbol,LVS_EX_GRIDLINES| LVS_EX_FULLROWSELECT );
}
break;
case WM_NOTIFY:
{
...
int i;
LVFINDINFO plvfi={0};
plvfi.flags=LVFI_STRING;
plvfi.psz=lpfr->lpstrFindWhat;
i=ListView_FindItem(hWnd,-1,&plvfi);//returning 0 for any item
DWORD dw=GetLastError();//Returning 0
ListView_SetItemState(hWnd,i,LVIS_FOCUSED|LVIS_SELECTED,0x000F);//For selcting that item
}
...
Isn't hWnd a dialog handle? Why do you use it rather than hWndListViewSymbol?
Re: Item search in Listview control
Quote:
Originally Posted by
VictorN
Isn't hWnd a dialog handle? Why do you use it rather than hWndListViewSymbol?
Ya,i done mistake,I was written wrong.It should be hWndListViewSymbol.Thanks alot.But one more thing how to search subitems in a list view control,as like item saerching?
Re: Item search in Listview control
Quote:
Originally Posted by
manjut19
... how to search subitems in a list view control,as like item saerching?
There is no any message nor API to search for a subitem.
You have to implement such a search yourself. For example get subitem text (ListView_GetItemText) in a loop and compare it with the pattern you need to look for.
Re: Item search in Listview control
Quote:
Originally Posted by
VictorN
There is no any message nor API to search for a subitem.
You have to implement such a search yourself. For example get subitem text (ListView_GetItemText) in a loop and compare it with the pattern you need to look for.
Ok,Thanks alot I will try for that
Re: Item search in Listview control
One more qustuion,After Getting the index of searched item ,i have to make it as selected,But the same time previosly selected row should be deslected.How to deselect a perticular row?
Code:
if ((lpfr->Flags & FR_FINDNEXT))
{
int iIndex;
LVFINDINFO plvfi={0};
plvfi.flags=LVFI_STRING;
plvfi.psz=lpfr->lpstrFindWhat;
iIndex=ListView_FindItem(hWndListViewSymbol,-1,&plvfi);
SetFocus(hWndListViewSymbol);
ListView_SetItemState(hWndListViewSymbol,iIndex,LVIS_FOCUSED|LVIS_SELECTED,0x000F);}
Re: Item search in Listview control
To unselect item just pass in yero as a new item state:
Code:
ListView_SetItemState(hWndListViewSymbol, iIndex, 0, 0x000F);}