Mirror

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


Problem/Question/Abstract:

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.

Answer:

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);
begin
  {Get Current Row and Column Values for Richedit Control}
  with re1 do
  begin
    Row := sendMessage(handle, EM_LINEFROMCHAR, Selstart, 0);
    Col := selstart - sendmessage(handle, EM_LINEINDEX, row, 0);
  end;
end;

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

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

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