Mega Code Archive

 
Categories / Delphi / VCL
 

How to automatically scroll in a tRichEdit after FindText finds a position outside the displayed text

Title: How to automatically scroll in a tRichEdit after FindText finds a position outside the displayed text ?? Question: Sometimes TRichEdit.FindText returns a position in the text that is beyond the current display. The following example shows how to automatically scroll in the TRichEdit, so that the found text is displayed. Answer: This procedure searches a given text in a RichEdit control. If the found text is outside the currently displayed portion of the text, the RichEdit automatically scrolls so the found text is displayed. procedure Find_a_Text(TheText: string); var FoundPos: integer; begin // Perfom the search action, foundpos is the starting point of the found text FoundPos := RichEdit1.FindText(TheText, 0, Length(RichEdit1.Text), []); if not (FoundPos then begin // These show found text as selected RichEdit1.SelStart := FoundPos; RichEdit1.SelLength := Length(TheText); // If the found text is outside the displayed portion of the text // this instruction performs an automatic scroll. RichEdit1.Perform(EM_SCROLLCARET, 0, 0); // Make sure the control is redrawn RichEdit1.Invalidate; end; end;