Solved

TMemo??!!

Posted on 2001-08-02
11
381 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
  • 3
  • 3
  • 3
  • +1
11 Comments
 
LVL 5

Expert Comment

by:scrapdog
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:ivylnm
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 21

Expert Comment

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

Expert Comment

by:gemarti
Comment Utility
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
Comment Utility
//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
Comment Utility
Thanks for providing such useful information which really solves my problem.
0
 
LVL 3

Expert Comment

by:rondi
Comment Utility
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
Comment Utility
Ok rondi.  Thanks very much for the additional information.  It really does me a lot of help!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now