Adapt Height of a form depending on a TMemo text content

On a TForm I have a TMemo
I fill in the TMemo lines with some text (variable) (wordwrap is true)
I want to programmatically adapt the height of the form so that the entire text is visible (no scrollbars and anchor is adapted)
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.

Sinisa VukSoftware architectCommented:
I made simple example...
procedure TForm1.Button1Click(Sender: TObject);
  rc: TRect;
  dc: HDC;
  SaveFont: HFont;
  s: String;
  ZeroMemory(@rc, SizeOf(TRect));
  //get dc
  dc := GetDC(Memo1.Handle);
  SaveFont := SelectObject(dc, Memo1.Font.Handle);
  //get calculated rect
  s := Memo1.Text;
  DrawText(dc , PAnsiChar(s), Length(s), rc, DT_CALCRECT or DT_NOCLIP );   
  //restore old
  SelectObject(dc, SaveFont);
  ReleaseDC(Memo1.Handle, dc);
  //set memo size
  Memo1.ClientHeight := (rc.Bottom  - rc.Top);
  Memo1.ClientWidth := (rc.Right  - rc.Left);

  Form1.ClientHeight := Memo1.Top + Memo1.Height + 5;

Open in new window

Note... text is already word-wrapped.

More extended (and complex) example could be find here in a one old similar EE topic...

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
Geert GOracle dbaCommented:
what happens if the text doesn't fit on the entire screen ?

wanting the form to be larger than the screen might give problems
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.