Solved

TMemo??!!

Posted on 2001-08-02
11
386 Views
Last Modified: 2010-04-06
I am planning to have a memo where user can only enter 4 lines of words.  How am I going to control this?
0
Comment
Question by:ivylnm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
  • +1
11 Comments
 
LVL 5

Expert Comment

by:scrapdog
ID: 6343656
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
 
LVL 5

Expert Comment

by:scrapdog
ID: 6343692
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
 
LVL 21

Expert Comment

by:gemarti
ID: 6346018
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ivylnm
ID: 6347059
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
 
LVL 5

Expert Comment

by:scrapdog
ID: 6347113
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
 
LVL 21

Expert Comment

by:gemarti
ID: 6347628
Okay so now we know the height of the memo what is its width?
0
 
LVL 21

Expert Comment

by:gemarti
ID: 6347744
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
 
LVL 3

Accepted Solution

by:
rondi earned 100 total points
ID: 6348143
//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
 

Author Comment

by:ivylnm
ID: 6350748
Thanks for providing such useful information which really solves my problem.
0
 
LVL 3

Expert Comment

by:rondi
ID: 6351161
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
 

Author Comment

by:ivylnm
ID: 6354367
Ok rondi.  Thanks very much for the additional information.  It really does me a lot of help!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi XE10 Round Image 2 227
QRReport  TQrmemo vertical stretching 1 68
Tembedded WB animatid gifs not animated on some pcs 2 94
Browsing a TTreeView in Delphi 5 49
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question