• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 262
  • Last Modified:

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?
0
MartinC
Asked:
MartinC
1 Solution
 
ginsonicCommented:
procedure TForm1.Button1Click(Sender: TObject);
var
  rect1, rect2: TRect;
  S: String;
begin
  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;
end;
0
 
MartinCAuthor Commented:
ginsonic:  Thanks, that worked. I don't for one minute understand it, but it worked. Points!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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