Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 249
  • Last Modified:

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.

Thanks

Mark
0
Mark Brady
Asked:
Mark Brady
1 Solution
 
emadatCommented:
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.
0
 
LukA_YJKCommented:
I remember in Richedit there is an Event OnRequestResize, maybe it would be a more stable solution?
0
 
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);
begin
if button1.caption = 'Add Data' then
begin
button1.caption := 'Clear';
memo1.lines.loadfromfile ('c:\bonusplus\data\Mytext.txt');
memo1.Height := (memo1.Lines.Count + 1) * canvas.TextHeight('a') + 10;

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


end.


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.

Mark
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now