Question

I am implementing a simple MFC text editor and I have run into trouble with my find function. Specfically in my 'find box' dialog class I have the following code:

FINDTEXTEX ft;
ft.chrg.cpMin = 0;
ft.chrg.cpMax = -1;
ft.lpstrText = _T("wallaby");
long n = pmyRichEditCtrl->FindText(FR_MATCHCASE|FR_WHOLEWORD, &ft);
if (n != -1)
   pmyRichEditCtrl->SetSel(ft.chrgText);

However, n is always -1 even when the word wallaby is typed into the control. Any help would be greatly appreciated.

Was it helpful?

Solution

It all depends where your current cursor selection is. If you typed the word then most likely your cursor will be positioned directly after the typed word. If you do not care about the position of the cursor then you can set the position to the beginning and start finding the text from the beginning:

pmyRichEditCtrl->SetSel( 0, 0 );
long n = pmyRichEditCtrl->FindText(FR_DOWN|FR_MATCHCASE|FR_WHOLEWORD, &ft);

Also, do not forget to set FR_DOWN parameter to search forward. If this parameter is not set it will search backward from FINDTEXTEX.chrg.cpMin:

Microsoft Rich Edit 2.0 and later: If set, the search is forward from FINDTEXTEX.chrg.cpMin; if not set, the search is backward from FINDTEXTEX.chrg.cpMin.

Microsoft Rich Edit 1.0: The FR_DOWN flag is ignored. The search is always forward.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top