-
November 9th, 2013, 01:34 AM
#1
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;
}
-
November 9th, 2013, 04:18 AM
#2
Re: Item search in Listview control
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.
Victor Nijegorodov
-
November 9th, 2013, 06:59 AM
#3
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?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
November 10th, 2013, 05:58 AM
#4
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).
-
November 18th, 2013, 12:34 AM
#5
Re: Item search in Listview control
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?
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.
-
November 18th, 2013, 03:32 AM
#6
Re: Item search in Listview control
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?
Victor Nijegorodov
-
November 18th, 2013, 04:16 AM
#7
Re: Item search in Listview control
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,
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??
-
November 18th, 2013, 04:28 AM
#8
Re: Item search in Listview control
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"?
Victor Nijegorodov
-
November 18th, 2013, 04:36 AM
#9
Re: Item search in Listview control
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
-
November 18th, 2013, 04:49 AM
#10
Re: Item search in Listview control
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?
Victor Nijegorodov
-
November 18th, 2013, 04:56 AM
#11
Re: Item search in Listview control
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?
-
November 18th, 2013, 05:03 AM
#12
Re: Item search in Listview control
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.
Victor Nijegorodov
-
November 18th, 2013, 05:12 AM
#13
Re: Item search in Listview control
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
-
November 18th, 2013, 05:47 AM
#14
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);}
-
November 18th, 2013, 05:59 AM
#15
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);}
Victor Nijegorodov
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|