Recent

Author Topic: To limit the characters in a line of TMemo  (Read 4688 times)

asdf

  • Sr. Member
  • ****
  • Posts: 310
To limit the characters in a line of TMemo
« on: March 27, 2011, 07:24:36 am »
In my project, I will let the user to describe in TMemo with the limit of 150 characers per line
but no limit of lines. How can I do that ?

And what is the technique to pick up each sentence from such Tmemo ?
« Last Edit: March 27, 2011, 07:26:40 am by asdf »
Lazarus 1.2.4 / Win 32 / THAILAND

TurboRascal

  • Hero Member
  • *****
  • Posts: 672
  • "Good sysadmin. Bad programmer."™
Re: To limit the characters in a line of TMemo
« Reply #1 on: March 27, 2011, 09:08:28 am »
A quick and simple way to limit typed text:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Memo1KeyPress(Sender: TObject; var Key: char);
  2. begin
  3.   if (Length(Memo1.Text) >= 150) and (key <> #08) then
  4.    key := #0
  5. end;      

While cursor and delete keys work when blocked, backspace (#08) does not so I added a check for it too.

This however is far from perfect and some more work is required; the problem is that you can paste text at will. To use this properly, you should either disable pasting or check what is being pasted...
Regards, ArNy the Turbo Rascal
-
"The secret is to give them what they need, not what they want." - Scotty, STTNG:Relics

asdf

  • Sr. Member
  • ****
  • Posts: 310
Re: To limit the characters in a line of TMemo
« Reply #2 on: March 28, 2011, 06:04:01 am »
The user may enter data into TMemo in many lines.
But in each line, I would like to limit only 150 characters.
Lazarus 1.2.4 / Win 32 / THAILAND

typo

  • Hero Member
  • *****
  • Posts: 3051

 

TinyPortal © 2005-2018