Height of a TMemo - fit to its text?

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?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MartinCAuthor Commented:
ginsonic:  Thanks, that worked. I don't for one minute understand it, but it worked. Points!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.