Mega Code Archive

 
Categories / Delphi / Examples
 

Setting the caret in a RichEdit

Title: Setting the caret in a RichEdit. Question: Getting the caret position in a RichEdit is no problem (richedit.getcaret). But how to set the caret? Answer: That's: Procedure setline(WhichEdit:TRichedit;Linepos,charpos:integer); Begin with WhichEdit do begin selstart:=perform(EM_LineIndex,Linenum,0)+charpos; perform(EM_ScrollCaret,0,0); end; end; Comments: The EM_ScrollCaret function can be ommited if you dont want to scroll to the position of the caret. This procedure can also be used in a TMemo but then the witchedit definition should be changed to TMemo. In addition this procedure can also be used to answer the question of article 952 (How to set the focus to a particular line in Memo component?) in a more elegant way. For this you would have to add the following line of code after the selstart line: sellength:=length(lines(line)); And set charpos to 0. I hope that someone can find a usefull purpose for this procedure.