Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to search for words in a rich edit control and i used the EM_FINDTEXTW message for the purpose. but it is always returning zero even if the particular word is there in the edit box.. Why is it so?

C++
// i have created the edit control using the below code 
LoadLibrary("Msftedit.dll");
	HWND hEdit2 = CreateWindowEx(WS_EX_CLIENTEDGE,
		"RICHEDIT50W",
		"My Rich Edit",
		WS_VISIBLE | WS_BORDER |
		WS_CHILD |
		ES_MULTILINE | WS_EX_RIGHTSCROLLBAR | WS_VSCROLL | WS_SIZEBOX,
		500, 30,
		500, 500,
		hMAin,
		0,
		GetModuleHandle(NULL),
		NULL);
SetWindowText(hEdit2, buffer);


//searching for a particular word
FINDTEXTEX ft;
	CHARRANGE ch;
	ch.cpMin = 0;
	ch.cpMax = -1;
	ft.chrg = ch;
	ft.lpstrText = m_szWord;
	int nIndex = SendMessage(hEdit2 , EM_FINDTEXTEX, FR_WHOLEWORD, (LPARAM)&ft);
	CString s;
	s.Format("%d", nIndex);
	MessageBox(hMAin, s, "", NULL);


m_szWord contains the word i want to search in the edit box. MessageBox always shows the value zero.
Please Help..
Posted
Updated 12-Jan-15 22:58pm
v6
Comments
Kornfeld Eliyahu Peter 13-Jan-15 5:13am    
From MSDN:
<pre>
Return value

If the target string is found, the return value is the zero-based position of the first character of the match.
If the target is not found, the return value is –1.
</pre>
So 0 means that the text found at the first position (index 0) and not means error!
Nimsha KV 13-Jan-15 5:47am    
But the word is not at the beginning.. I tried giving different words..but still it is returning 0.
Jochen Arndt 13-Jan-15 5:23am    
EM_FINDTEXTW requires using the Unicode structure and passing a Unicode string. But your code snippet is not Unicode.

All EM_FINDTEXTxxx messages return -1 when the content is not found. Zero indicates that the word has been found at the begin of the text.
Nimsha KV 13-Jan-15 5:47am    
But the word is not at the beginning.. I tried giving different words..but still it is returning 0.
Jochen Arndt 13-Jan-15 5:55am    
Just to be sure:
Your application is not a Unicode build and m_szWord is not Unicode?

Also:
What are the values of ft.chrgText upon return?

1 solution

I tested below code and its working. Compare what is wrong at your end

LoadLibrary(L"Msftedit.dll");

hEdit2 = CreateWindowEx(WS_EX_CLIENTEDGE,
   L"RICHEDIT50W",
   L"My Rich Edit",
   WS_VISIBLE | WS_BORDER |
   WS_CHILD |
   ES_MULTILINE | WS_EX_RIGHTSCROLLBAR | WS_VSCROLL | WS_SIZEBOX,
   500, 30,
   500, 500,
   hWnd,
   0,
   GetModuleHandle(NULL),
   NULL);
   SetWindowText(hEdit2, L"This is Hello World");

   FINDTEXTEX ft;

   CHARRANGE ch;
   ch.cpMin = 0;
   ch.cpMax = -1;
   ft.chrg = ch;
   ft.lpstrText = L"Hello";
   nIndex = SendMessage(hEdit2 , EM_FINDTEXTEX, FR_DOWN, (LPARAM)&ft);
 
Share this answer
 
Comments
Nimsha KV 14-Jan-15 0:51am    
Now I got it correctly.. Thank you..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900