Resizing TStringGrid in Realtime lazarus / 31-LCL ---------------------------------------------------------------------- Knipfty · 2012-08-07 18:07 Hi All, I've been playing with the TStringGrid and didn't like the white space that would appear either to the right or bottom.  The row data can be variable while the column widths could also vary depending on the data.  Further, I didn't want to exceed 10 data rows or 11 rows counting the title.  I'm basically trying to keep the StringGrid in a tight space and trying to make it look good at the same time. I also could not find any methods or properties to help me control the white space properly so I wrote this little routine: Code: [Select]procedure ResizeStringGrid(SG: TStringGrid; Const MaxRows: integer); { RSK 07-Aug-2012 14:18 - 1. This procedure will accept a TStringGrid object and max rows to display. 2.  It will then determine if MaxRows < the row count.  If so, it assumes there will be a scroll bar at the bottom of the grid. 3. It then determines the real widths of the columns including any deividers and sets the Grid with to this value. 4. It then does the same with the row heights and set the grid to this value. This procedure assumes that the column widths are already set to how the program wants to display them.  If not, you may want to make a call to .AutoSizeColumns before calling this routine. } var    I,J: integer; begin   if MaxRows < SG.RowCount-1 then     J := 18  //Assumed Scrollbar Width.  I need to find out where this is stored.   else     J := 1;   for I := 0 to SG.ColCount-2 do     inc(J, SG.ColWidths[I]+SG.GridLineWidth);   SG.Width:=J+SG.BorderWidth;   J := -2;   for I := 0 to min(MaxRows, SG.RowCount-1) do     inc(J, SG.RowHeights[I]+SG.GridLineWidth);   SG.Height:=J; end; For me, it works well.  A couple of questions: 1. Is there something like this already in Lazarus that I just may have been unable to find? 2. Does anyone know where I can find the scollBar width used by TStringGrid?  Right now, through trial and error, I've settled on 18.  But that may not work in all cases. Knipfty ---------------------------------------------------------------------- Phil · 2012-08-07 19:14 Quote from: Knipfty on August 07, 2012, 08:07:11 pm1. Is there something like this already in Lazarus that I just may have been unable to find? 2. Does anyone know where I can find the scollBar width used by TStringGrid?  Right now, through trial and error, I've settled on 18.  But that may not work in all cases. I don't think there's any foolproof way of doing this. All you can try for is to make your grid presentable. For example, a little white space to the right of the last column is better than an unnecessary horizontal scroll bar because the last column did not quite fit. With Delphi VCL, you can set ClientWidth and ClientHeight to the sum of column widths / heights to fit them exactly, because these properties exclude the height of title bar (on forms), thickness of scroll bars, etc. But with cross-platform LCL, ClientWidth = Width and ClientHeight = Height, a compromise probably forced by an inability to determine scroll bar width, title bar height, etc. across all platforms. I think Windows title bar and scroll bars are generally taller/thicker than other platforms, so if you design for that worst case, it should be presentable on other platforms (just not a perfect fit). Apple's Cocoa control NSTableView has an option that you can use to tell the grid to widen the last column to take up any unused white space. I use that in most of my Cocoa apps. But for TStringGrid (or you) to do something like this, you would still have to know how much "client" space there is - and that's what's missing. Thanks. -Phil ---------------------------------------------------------------------- Blaazen · 2012-08-07 19:20 In your code is: Code: [Select]if MaxRows < SG.RowCount-1 then ... TStringGrid has readonly property "VisibleRowCount". Isn't it what you want to use instead of MaxRows ? ---------------------------------------------------------------------- Knipfty · 2012-08-07 19:35 Hi Blazaan, Actually no.  With MaxRows, I'm trying to control how far down I want the grid to grow.  So I state 10 max rows and there are 20 real rows, the routine will stop at 10 and the grid will automatically put the scroll bar on the right side. On the other hand, if I specify 10 max rows and the grid only 5 real rows, then the Grid will display with the 5 real rows, no scroll bar and still display nicely without any white space on the bottom. ---------------------------------------------------------------------- Knipfty · 2012-08-07 19:36 Hi Phil, And that's for that update.  The background is helpful. And to everyone else, feel free to use and adapt this to your needs. Knipfty