How to specify a line break in a TRichEdit (Views: 101)


I need to be able to (preferrably dynamically as the user is typing text) to specify a line break of say 70 characters so that the cursor will go to a new line upon reaching the 70 character limit. Actually it would be best to break on the last word boundary but even a break at 70 characters would give me a start.


I've had a play with this and the following is the best I could come up with quickly. At least it may give you a start:

Set a Variable called Backpace : Boolean = False ;

procedure GetCurrentRC(re1: TRichedit; var row, col: LongInt);
  {Get Current Row and Column Values for Richedit Control}
  with re1 do
    Row := sendMessage(handle, EM_LINEFROMCHAR, Selstart, 0);
    Col := selstart - sendmessage(handle, EM_LINEINDEX, row, 0);

procedure TForm1.re1SelectionChange(Sender: TObject);
  RTRow, RTCol: LongInt;
  GetCurrentRC(re1, RTRow, RTCol);
  if (rtCol = 70) and (not Backspace) then
    re1.Lines[rtRow] := Memo1.Lines[rtRow] + #13#10;

procedure TForm1.Re1KeyPress(Sender: TObject; var Key: Char);
  {If Backspacing we don't want it to jump down again}
  if key = #8 then
    backspace := True
    backspace := False;

I think that's about right. You would have to search on the position of any space if you wanted to break on a word boundary.

<< Back to main page