CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Apr 2013
    Posts
    77

    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;
    
    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: Item search in Listview control

    Quote Originally Posted by manjut19 View Post
    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
    1. ListView_FindItem cannot search for sub-items, only for items.
    2. ListView_FindItem returns the index of the item if successful, or -1 otherwise. So zero means the very first item was found.
    Victor Nijegorodov

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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)

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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).

  5. #5
    Join Date
    Apr 2013
    Posts
    77

    Re: Item search in Listview control

    Quote Originally Posted by 2kaud View Post
    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.

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: Item search in Listview control

    Quote Originally Posted by manjut19 View Post
    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

  7. #7
    Join Date
    Apr 2013
    Posts
    77

    Re: Item search in Listview control

    Quote Originally Posted by VictorN View Post
    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,Name:  table.png
Views: 2453
Size:  43.3 KB
    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??

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: Item search in Listview control

    Quote Originally Posted by manjut19 View Post
    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

  9. #9
    Join Date
    Apr 2013
    Posts
    77

    Re: Item search in Listview control

    Quote Originally Posted by VictorN View Post
    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

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: Item search in Listview control

    Quote Originally Posted by manjut19 View Post
    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

  11. #11
    Join Date
    Apr 2013
    Posts
    77

    Re: Item search in Listview control

    Quote Originally Posted by VictorN View Post
    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?

  12. #12
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: Item search in Listview control

    Quote Originally Posted by manjut19 View Post
    ... 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

  13. #13
    Join Date
    Apr 2013
    Posts
    77

    Re: Item search in Listview control

    Quote Originally Posted by VictorN View Post
    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

  14. #14
    Join Date
    Apr 2013
    Posts
    77

    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);}

  15. #15
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    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

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured