Board index » delphi » Max Length for Memo Lines?

Max Length for Memo Lines?

Is there a way to limit the length of each line in a Memo component? I
think it's possible to cap the max number of characters in an Edit
box, but I really need it for a memo component. I know you can set
MaxLength for the entire text, but I need to limit each line to 60
characters. Thanks if anyone can help me.
 

Re:Max Length for Memo Lines?


"Ken P." schrieb:

Quote

> Is there a way to limit the length of each line in a Memo component?

No, AFAIK, unless you write your own component, which controls the items
itself and does not let Windows do the job as TMemo does.

-Michael

Re:Max Length for Memo Lines?


Quote
Ken P. <a...@123.net> wrote in message

news:y0kpOBXEIPi8TygagAZkF3EUgzzA@4ax.com...

Quote
> Is there a way to limit the length of each line in a Memo component? I
> think it's possible to cap the max number of characters in an Edit
> box, but I really need it for a memo component. I know you can set
> MaxLength for the entire text, but I need to limit each line to 60
> characters. Thanks if anyone can help me.

Quite simple, actually...

procedure TForm1.Memo1KeyPress(Sender: TObject; var Key: Char);
var LineNo : integer;
begin
  LineNo := Memo1.Perform(EM_LINEFROMCHAR, Memo1.SelStart, 0);
  if (length(Memo1.Lines[line]) >= 10) and not((key = #13) or (key = #8))
then
  begin
   messagebeep(0);
   key := #0;
  end;
end;

There are other checking you can implement, eg. trim the current line if the
number of characters are more than say 60 because the user pasted some text
etc.

--
Christo Crause
Institute for Thermal Separations Research
Dept. Chem. Eng.
University of Stellenbosch
South Africa

Re:Max Length for Memo Lines?


Christo Crause schrieb:

Quote

> Ken P. <a...@123.net> wrote in message
> news:y0kpOBXEIPi8TygagAZkF3EUgzzA@4ax.com...
> > Is there a way to limit the length of each line in a Memo component?

> Quite simple, actually...

> procedure TForm1.Memo1KeyPress(Sender: TObject; var Key: Char);

Christo,

nice idea. But remember, you can also insert *some* lines of text from
clipboard, even using the mouse instead keyboard. This means you have to
check each line on each OnChange...

-Michael

Re:Max Length for Memo Lines?


On Thu, 11 Nov 1999 08:52:20 +0200, "Christo Crause"

Quote
<ccra...@ing.sun.ac.za> wrote:
>Ken P. <a...@123.net> wrote in message
>news:y0kpOBXEIPi8TygagAZkF3EUgzzA@4ax.com...
>> Is there a way to limit the length of each line in a Memo component? I
>Quite simple, actually...

>procedure TForm1.Memo1KeyPress(Sender: TObject; var Key: Char);
>var LineNo : integer;
>begin
>  LineNo := Memo1.Perform(EM_LINEFROMCHAR, Memo1.SelStart, 0);
>  if (length(Memo1.Lines[line]) >= 10) and not((key = #13) or (key = #8))
>then
>  begin
>   messagebeep(0);
>   key := #0;
>  end;
>end;

>There are other checking you can implement, eg. trim the current line if the
>number of characters are more than say 60 because the user pasted some text
>etc.

Thanks very much. It works perfectly, except for the 'paste from
clipboard' problem which does throw a wrench into the works. What is
the best way to limit the line length if the user pastes text in?
Since there's no 'OnPaste' event, I tried putting checks in the
'OnChange' event, but it doesn't work.

Anything you could tell me is appreciated.

Other Threads