Solved

TMemo??!!

Posted on 2001-08-02
11
387 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month9 days, 21 hours left to enroll

624 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