Link to home
Start Free TrialLog in
Avatar of ivylnm
ivylnm

asked on

TMemo??!!

I am planning to have a memo where user can only enter 4 lines of words.  How am I going to control this?
Avatar of scrapdog
scrapdog
Flag of United States of America image

Here is a simple...use the code in the OnChange of the Memo:



procedure TForm1.Memo1Change(Sender: TObject);
begin
    while Memo1.Lines.Count > 4 do
        Memo1.Lines.Delete(4);
end;
Sorry for all the typos.  What I meant is "Here is a simple way".

This code will also work if the user pastes something on to the memo.
You could set the Maximum Width of the memo field on your form and then figure out how many characters (X's or 1) can fit on four lines. Then set MaxLength to that number.

Memo1.MaxLength := SomeNumber;
Avatar of ivylnm
ivylnm

ASKER

I have tried the way scrapdog mentioned.  But the problem is user can still press <Enter> to go to the fifth line, although user cannot enter any character.  Is there any way to limit the TMemo to only 4 lines.  As now the TMemo's height is same as 4 lines only, I wish to have this TMemo not able to enter to fifth line . Any idea?

In this TMemo, two conditions I need to have : Only 4 lines for user to key in, and only 100 characters available.

Please help!
Here's something that might accomplish what you want; 20 points is not enough to go too much deeper than this. =)

This does have some artifacts.

Also make sure you set the Memo's MaxLength to 100.


procedure TForm1.Memo1Change(Sender: TObject);
var
   CPos :integer;
begin
   Cpos := SendMessage(Memo1.Handle,EM_LINEINDEX,4,0) - 1;
   if Memo1.SelStart > Cpos then begin
      Memo1.SelStart := Cpos;
      Memo1.SelLength := 0;
   end else if Memo1.SelStart + Memo1.SelLength > Cpos then
      Memo1.SelLength := CPos - Memo1.SelStart + 1;

   while (Memo1.Lines.Count > 4) do Memo1.Lines.Delete(4);
end;
Okay so now we know the height of the memo what is its width?
The more I think about this problem the more involved it gets.
So far I have thought about all the things you would have to know upfront..

1. Font type
2. Font Size
3. Font Style
4. Upper/Lower Case or Both
5. Memo Height
6. Memo Width
7. Maximum Length (i.e. 100)

Now Scrapdog has the right idea to some extent, but I would not want to just up and delete text. I'd rather just prohibit any more text from being entered into the field.

Come to think of it. If you only want 100 characters in the field why not just use an Edit component?

GEM
ASKER CERTIFIED SOLUTION
Avatar of rondi
rondi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ivylnm

ASKER

Thanks for providing such useful information which really solves my problem.
no sweat, ivy :)

One thing though, with wordwrap false, the code above does't stop horizontal scrolling if the user pastes the text instead of typing it.

You could prevent Ctrl+V pasting by handling OnKeyDown like this:

procedure TForm1.Memo1KeyDown(Sender: TObject; var Key: integer; Shift: TShiftState);
begin
  if (Key = Ord('V')) and (ssCtrl in Shift) then
    Key := 0;
end;

Then disable/replace that default TMemo/TEdit popup menu.

rondi.
Avatar of ivylnm

ASKER

Ok rondi.  Thanks very much for the additional information.  It really does me a lot of help!