Page 1 of 1

CR+LF TRichEdit to TRichView

Posted: Sat Jan 28, 2006 10:22 pm
by Shinichiro
Please teach a method to CR+LF add TRichView

////////////////////////////////////////////////////////////////////////////////////
procedure AddURL(S: String; RV: TRichView; DefStyle, UrlStyle: Integer);
var
Before, CurrentWord, Space: String;
P: Integer;
begin
Before := '';

if S = '' then
begin
RV.Add('', DefStyle);
exit;
end;

while S <> '' do
begin
P := Pos(' ', S);
if P = 0 then
P := Length(S) + 1;

CurrentWord := Copy(S, 1, P-1);
Space := Copy(S, P, 1);
S := Copy(S, P+1, Length(S));

if URL(CurrentWord) or Email(CurrentWord) then
begin
if Before <> '' then
begin
RV.Add(Before, DefStyle);
Before := '';
end;

RV.Add(CurrentWord, UrlStyle);

if Space <> '' then
RV.Add(Space, DefStyle);
end
else
Before := Before + CurrentWord + Space;
end;

if Before <> '' then
RV.Add(Before, DefStyle);
end;

procedureTForm2.Button1Click(Sender: TObject);
var
Comment : String;
begin
Form1.RichView1.Format;
Comment := RichEdit1.text;
AddURL(Comment, Form1.RichView1, 0, 1);
Form1.RichView1.FormatTail;
end;
////////////////////////////////////////////////////////////////////////////////////

Posted: Sat Jan 28, 2006 10:40 pm
by Sergey Tkachenko
Do you want to modify this function to allow CR+LF in the S parameter?

Posted: Sat Jan 28, 2006 11:13 pm
by Shinichiro
Yes, I want to modify this function to allow CR+LF in the S parameter.
What should it carry out?

Posted: Sun Jan 29, 2006 7:31 pm
by Sergey Tkachenko
The simplest way is to call AddURL for each line.

Code: Select all

var p: Integer;
    Line, Comment: String;

Comment := AdjustLineBreaks(Comment);
repeat
  p := Pos(#13, Comment);
  if p=0 then begin
    Line := Comment;
    Comment := '';
    end
  else begin
    Line := Copy(Comment, 1, p-1);
    Comment := Copy(Comment, p+2, Length(Comment));
  end;
  AddURL(Line, Form1.RichView, 0, 1);
until Comment='';
Form1.RichView.FormatTail;
(do not call Format each time before executing this code, it not necessary, and only slows it down)