Forum > LCL
TStringGrid with stretched rows heights according to content of cells
LacaK:
I would like to show content of table like it is done in HTML pages. Rows are stretched to accomodate its content.
So table row height is set to greater height of cell in the given row.
Is there any effective way how to achieve similar behavior in any components placed on TForm?
For example using TStringGrid, where multiline content in cells is wrapped and row heights are adjusted to show whole content (without clipping)
dsiders:
--- Quote from: LacaK on January 26, 2021, 07:03:28 pm ---I would like to show content of table like it is done in HTML pages. Rows are stretched to accomodate its content.
So table row height is set to greater height of cell in the given row.
Is there any effective way how to achieve similar behavior in any components placed on TForm?
For example using TStringGrid, where multiline content in cells is wrapped and row heights are adjusted to show whole content (without clipping)
--- End quote ---
This discussion might be of interest:
https://forum.lazarus.freepascal.org/index.php?topic=35488.0
jamie:
maybe you can implement the onDrawCell event where you can adjust cell size to min requirement..
LacaK:
Thank you for your suggestions.
I'm done with this so far:
Calculate height of a row:
--- 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";}};} --- { TStringGridHelper } TStringGridHelper = class helper for TStringGrid protected procedure AutoSizeRow(aRow: integer); end; { TStringGridHelper } procedure TStringGridHelper.AutoSizeRow(aRow: integer);var aCanvas: TCanvas; aCol, maxRowHeight:integer; aText: String; textRect: TRect;begin aCanvas := GetWorkingCanvas(Canvas); maxRowHeight := DefaultRowHeight; for aCol:=0 to ColCount-1 do begin aText := Cells[aCol,aRow]; textRect := Rect(0, 0, ColWidths[aCol], MaxInt); DrawText(aCanvas.Handle, PChar(aText), Length(aText), textRect, DT_CALCRECT or DT_WORDBREAK); if maxRowHeight < textRect.Height then maxRowHeight := textRect.Height end; if aCanvas<>Canvas then FreeWorkingCanvas(aCanvas); RowHeights[aRow] := maxRowHeight+2;end;
and then draw cells:
--- 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";}};} ---procedure TMyForm.MyStringGridDrawCell(Sender: TObject; aCol, aRow: Integer; aRect: TRect; aState: TGridDrawState);var aText: String; textStyle: TTextStyle;begin with MyStringGrid do begin aText := Cells[aCol,aRow]; textStyle:=Canvas.TextStyle; textStyle.Layout:=tlTop; textStyle.SingleLine:=False; textStyle.Wordbreak:=True; Canvas.FillRect(aRect); Canvas.TextRect(aRect, ARect.Left+1, ARect.Top+1, aText, textStyle); end;
I think, that this automatic row height sizing will be useful extension to TStringGrid 8)
PasCoder:
Dear Lacak, Greetings to you!
I'm a newbie in Pascal. Can you help know how I can use your code in my Project.
It seems its a class but I've not known how methods, Properties and Procedures are called in Pascal. It seems to be a little bit different from Microsoft .Net Framework style. Please, can you help me? A simple Project would be of a big advantage to me.
Thanks
Navigation
[0] Message Index
[#] Next page