Height of a TMemo - fit to its text?

Posted on 2004-11-20
Last Modified: 2010-04-05
How can I make a created TMemo component get the required height to fit a certain
amount of text in? I am reading from a database the content of a field to the TMemo's
Text and I would like the created TMemo's height to be just enough to contain the text
it will be displaying. I was hoping there was some kind of autosize but apparently
not. I am creating a series of these TMemos one beneath each other, so it really
has to be done on the fly. Any ideas?
Question by:MartinC
    LVL 9

    Accepted Solution

    procedure TForm1.Button1Click(Sender: TObject);
      rect1, rect2: TRect;
      S: String;
      s := Memo1.Text;
      memo1.Perform( EM_GETRECT, 0, longint(@rect1));
      rect2 := rect1;
      canvas.font := memo1.font;
      DrawTextEx( canvas.handle, Pchar(S), Length(S), rect2, DT_CALCRECT or
                             DT_EDITCONTROL or DT_WORDBREAK or DT_NOPREFIX, Nil );
      memo1.Height := memo1.height + rect2.bottom - rect1.bottom;

    Author Comment

    ginsonic:  Thanks, that worked. I don't for one minute understand it, but it worked. Points!

    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    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 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…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    754 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

    23 Experts available now in Live!

    Get 1:1 Help Now