Board index » delphi » Indenting in memo or richedit?

Indenting in memo or richedit?

Hi,

Is it possible to get the same indenting behaviour as in the Delphi editor
into a richedit or a memo? When I press enter for a new row I want the
cursor to be positioned on the same col as the row above.

Any hints or code snippets appreciated.

Thanks,
PM

 

Re:Indenting in memo or richedit?


Quote
In article <8mbhqo$3...@bornews.borland.com>, Pm wrote:
> Is it possible to get the same indenting behaviour as in the Delphi editor
> into a richedit or a memo? When I press enter for a new row I want the
> cursor to be positioned on the same col as the row above.

Implement smart indent in a rich edit control:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    RichEdit1: TRichEdit;
    procedure RichEdit1KeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses richedit;
{$R *.DFM}

procedure TForm1.RichEdit1KeyPress(Sender: TObject; var Key: Char);
var
  line, col, indent: integer;
  S: String;
begin
  if key = #13 then begin
    key := #0;
    with sender as trichedit do begin
      // figure out line and column position of caret
      line := PerForm( EM_EXLINEFROMCHAR,0, SelStart );
      Col  := SelStart - Perform( EM_LINEINDEX,line,0 );

      // get part of current line in front of caret
      S:= Copy( lines[ line ], 1, col );

      // count blanks and tabs in this string
      indent := 0;
      while (indent < length( S )) and
            (S[indent+1] In  [' ',#9])
      do
        Inc( indent );

      // insert a linebreak followed by the substring of blanks and tabs
      SelText := #13#10+Copy(S, 1, indent);
    end;
  end;
end;

end.

Peter Below (TeamB)  100113.1...@compuserve.com)
No e-mail responses, please, unless explicitly requested!

Other Threads