Mega Code Archive

 
Categories / Delphi / Algorithm Math
 

Search text in your MEMO

Title: Search text in your MEMO Question: How can i search text in a Memo like MS Word? Answer: In many of my applications I must to work with MEMOS and accomplish search of text with the following procedure: 1. Put a Memo, this too is valid for a DBMEMO component. 2. Put a FindDialog component. 3. In the event OnFind of the component FindDialog put the following code: procedure TForm1.FindDialog1Find(Sender: TObject); var Buffer, Pos, tPointer : PChar; BuffLength : Word; begin With Sender as TFindDialog do begin GetMem(tPointer, Length(FindText) + 1); StrPCopy(tPointer, FindText); BuffLength:= Memo1.GetTextLen + 1; GetMem(Buffer,BuffLength); Memo1.GetTextBuf(Buffer,BuffLength); Pos:= Buffer + Memo1.SelStart + Memo1.SelLength; Pos:= StrPos(Pos, tPointer); if Pos = NIL then MessageBeep(0) else begin Memo1.SelStart:= Pos - Buffer; Memo1.SelLength:= Length(FindText); end; FreeMem(tPointer, Length(FindText) + 1); FreeMem(Buffer,BuffLength); Memo1.SetFocus; //This line highlight the found text end; end; 4. Call to the FindDialog1.Execute and see the results. I hope this serve to you Regards