Forum > General
Automatically setting the height of a Memo
(1/1)
seany:
As stated previously, my goal is to make a markdown renderer --> with some additional functionality, such as colors, and some groff/troff capacity.
So I have this code:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---if (lineType = renderTarget) then begin showMessage('good to go - this will be a target to render'); // add line currSection.isRendered := False; // NOT YET Rendered // The following attributes and details need to be considered for a successful render // #1 The Text Content SetLength(currSection.Text,0); // TEXT ARRAY is set to empty SetLength(currSection.Text, length(currSection.Text) + 1); // add one element currSection.Text[length (currSection.Text) -1 ] := currLine ; // This is the current text to be rendered // #2 The Text COLOR if (currColor = clNone) then begin currSection.TextColor:= colorLabel.Color; // have set the global color end else begin currSection.TextColor:= currColor; // have set the global color end; // Document.DocumentTree.appendChild(currsection); // Under development // render line SetLength(memoList , Length(memoList) + 1); // increase size of render container by 1 memoList[length(memoList) -1 ] := TMemo.Create(self); // new memo created memoList[length(memoList) -1].Parent := renderPanel; memoList[length(memoList) -1].Color := currSection.TextColor; // backgroundColor memoList[length(memoList) -1].Left := 10; memoList[length(memoList) -1].Width := renderPanel.Width - ScrollBar2.Width - memoList[length(memoList) -1].Left ; memoList[length(memoList) -1].Text := currSection.Text[0]; // memoList[length(memoList) -1].Height := NEED SOME MAGIC HERE end;
I have this text:
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
I want to find out programmatically the height of the memo that is necessary so that no scrolling would be required. I looked for things like TMemo.textHeight, but can't find it by a simple googling. Please help. Thank you.
WooBean:
Hi,
for start try to use self.Memo1.Font.GetTextHeight('Ay') to get height of a single line and then multiply it by Memo1.Lines.Count.
WooBean:
Well, seany
the goals mentioned at starting post (as coloring and formatting text) will need more capable component than TMemo is. Switch to RichMemo (use Online Package Manager and install it in Lazarus IDE).
As far as you are trying to calculate Memo (TMemo) height needed to display some text lines you can use code below (only for Windows):
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses ... windows; procedure TForm1.Memo1Change(Sender: TObject);var LineHeight: Integer; DC: HDC; SaveFont : HFont; Metrics : TTextMetric; LC,dy: Integer;begin //self.label1.caption:=inttostr(self.Memo1.Font.GetTextHeight('A')); //=16 DC := GetDC(Memo1.Handle); SaveFont := SelectObject(DC, Memo1.Font.Handle); getTextMetrics(DC, Metrics); SelectObject(DC, SaveFont); ReleaseDC(Memo1.Handle, DC); LineHeight := Metrics.tmHeight; //=17 dy:=2*Metrics.tmDescent; //=2*3 LC := Memo1.Lines.Count; if LC < 1 then LC := 1; Memo1.Height := LC * LineHeight + dy;end;
Do not give up.
jamie:
"Drawtext" function can give u a rectangle needed using the complete text string from the memo
Navigation
[0] Message Index