Board index » delphi » Component for restricting number of lines

Component for restricting number of lines

Does anyone know a TMemo/TRichEdit component which can handle an input
for a given number of rows (lines) where each row (line) has sth. like a
MaxLength property?

i.e.: I need to define "text regions" like (for example) 50 chars * 20
lines.

Any hint is welcome
Luc.

 

Re:Component for restricting number of lines


Quote
In article <3BDC23C5.D10EA...@rol3.com>, Lucas Franzen wrote:
> Does anyone know a TMemo/TRichEdit component which can handle an input
> for a given number of rows (lines) where each row (line) has sth. like a
> MaxLength property?

> i.e.: I need to define "text regions" like (for example) 50 chars * 20
> lines.

Limiting a memo to 5 lines of input of max 25 characters each:

procedure TForm1.Memo1KeyPress(Sender: TObject; var Key: Char);
Var
  line, col: Integer;
begin
  With Sender As TMemo Do Begin
    line := Perform( EM_LINEFROMCHAR, SelStart, 0 );
    col  := SelStart - Perform(EM_LINEINDEX, line, 0 );
    If key = #8 Then Begin
      { Do not allow backspace if caret is on first column and
        deleting the linebreak of the line in front would result
        in a line of more than 25 characters. Damn inconvenient
        for the user but specs are specs... }
      If (col = 0) and (line > 0) Then Begin
        If (Length(lines[line])+Length(lines[line-1])) > 25 Then
          Key := #0;
      End; { If }
    End { If }
    Else If key In [#13,#10] Then Begin
      { Handle hard linebreaks via Enter or Ctrl-Enter }
      If lines.count >= 5 Then Begin
        { Max number of lines reached or exceeded, set caret
          to start of next line or this line, if on the last. }
        key := #0;
        If line = 4 Then
          SelStart := Perform( EM_LINEINDEX, line,0 )
        Else
          SelStart := Perform( EM_LINEINDEX, line+1,0 );
      End; { If }
    End { If }
    Else If Key >= ' ' Then Begin
      { Do swallow key if current line has reached limit. }
      If Length( lines[line] ) >= 25 Then
        Key := #0;
    End; { If }
  End; { With }
  If Key = #0 Then
    Beep;
end;

procedure TForm1.Memo1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var
  line, col: Integer;
begin
  If Key = VK_DELETE Then
    with Sender As TMemo do begin
      line := Perform( EM_LINEFROMCHAR, SelStart, 0 );
      col  := SelStart - Perform(EM_LINEINDEX, line, 0 );
      If col = Length( lines[line] ) Then
        If (line < 4) and ((Length(lines[line]) + Length(lines[line+1])) >
25)
        Then Begin
          key := 0;
          Beep
        End; { If }  
    end; { with }
end;

Peter Below (TeamB)  100113.1...@compuserve.com)
No e-mail responses, please, unless explicitly requested!
Note: I'm unable to visit the newsgroups every day at the moment,
so be patient if you don't get a reply immediately.

Re:Component for restricting number of lines


Peter,

as always: Asking a question and waiting for your input ...

Thank you very much and
Kind regards

Luc.

Other Threads