Solved

TMemo??!!

Posted on 2001-08-02
11
382 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
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

777 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