Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 391
  • Last Modified:

TMemo??!!

I am planning to have a memo where user can only enter 4 lines of words.  How am I going to control this?
0
ivylnm
Asked:
ivylnm
  • 3
  • 3
  • 3
  • +1
1 Solution
 
scrapdogCommented:
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;
0
 
scrapdogCommented:
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.
0
 
gemartiCommented:
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;
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
ivylnmAuthor Commented:
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!
0
 
scrapdogCommented:
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;
0
 
gemartiCommented:
Okay so now we know the height of the memo what is its width?
0
 
gemartiCommented:
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
0
 
rondiCommented:
//assuming that wordwrap is false and you don't mind horizontal scrolling:

-------------------------------------------------------

procedure TForm1.Memo1Change(Sender: TObject);
begin
  with Memo1.Lines do
  begin
    if Count > 4 then
    begin
      BeginUpdate;  //j.i.c it's a huge paste op.
      while Count > 4 do
        Delete(4);
      EndUpdate;
    end;
  end;
end;

procedure TForm1.Memo1KeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then Key := #0;
end;

-------------------------------------------------------

//assuming that wordwrap is false and you DO mind horizontal scrolling:

procedure TForm1.Memo1Change(Sender: TObject);
begin
  with Memo1.Lines do
  begin
    if Count > 4 then
    begin
      BeginUpdate;  //j.i.c it's a huge paste op.
      while Count > 4 do
        Delete(4);
      EndUpdate;
    end;
  end;
end;

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

  function MemoLine: integer;
  begin
    Result := Memo1.Perform(EM_LINEFROMCHAR, Memo1.SelStart, 0);
  end;

begin
  if (Key = #13) or (Form1.Canvas.TextWidth(Memo1.Lines[MemoLine]) > (Memo1.Width-4)) then
    Key := #0;
end;

-------------------------------------------------------
0
 
ivylnmAuthor Commented:
Thanks for providing such useful information which really solves my problem.
0
 
rondiCommented:
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.
0
 
ivylnmAuthor Commented:
Ok rondi.  Thanks very much for the additional information.  It really does me a lot of help!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now