Set memo1.height according to content (lines.count) problem

I have form1 set to autosize.  I want memo1 to resize (height only) according to how many lines are in it without having to use ssvertical.
In other words, when memo1 loads up (from text in a text file), if it has more that what will fit on the screen, I need to resize it accordingly so when I print form1, it will print all text on form and memo1.

In my code, I hide the buttons on the form before it prints then switch them back on.

I've tried // memo1.height := memo1.lines.count * 20;
but that doesn't work quite right.  It does resize it but not right.  My font is Tahoma 10 regular.


LVL 20
Mark BradyPrincipal Data EngineerAsked:
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.

Try this:

memo1.Height := memo1.Lines.Count * canvas.TextHeight('a') + 10;

You can experiment with the value 10 to find the value tha best suit you.

Aslo you can do the following:
iMemoHeight := memo1.Lines.Count * canvas.TextHeight('a')+ 10;
if iMemoHeight> iMyMaximamu then iMemoHeight := iMyMaximamu;
memo1.Height := iMemoHeight;
where iMyMaximamu is the maximum height for the memo.

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
I remember in Richedit there is an Event OnRequestResize, maybe it would be a more stable solution?
Mark BradyPrincipal Data EngineerAuthor Commented:
I've used your answer and modified it a little.  This is what I have now that works for me.

procedure TForm1.Button1Click(Sender: TObject);
if button1.caption = 'Add Data' then
button1.caption := 'Clear';
memo1.lines.loadfromfile ('c:\bonusplus\data\Mytext.txt');
memo1.Height := (memo1.Lines.Count + 1) * canvas.TextHeight('a') + 10;

button1.caption := 'Add Data';
memo1.Height := (memo1.Lines.Count + 1) * canvas.TextHeight('a') + 10;
if memo1.height > 490 then begin
memo1.height := 490;
memo1.ScrollBars := ssvertical;
memo1.ScrollBars := ssnone;


I had to add the '+ 1' so it would show every line in the memo but in any case, the problem is solved, thank you.

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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.