Mirror

Maximize the columns of a TStringGrid when the form resizes (Views: 708)


Problem/Question/Abstract:

When a form resizes, how do I resize the columns so they will occupy all available space? E.g. if stringgrid.width=100 and I have 4 columns, how do I adjust the columns width?

Answer:

procedure TForm1.FormResize(Sender: TObject);
var
  c: integer;
  colWidth: integer;
  lineWidths: integer;
  delta: integer;
begin
  Grid.Width := ClientWidth - 24; {or whatever}
  lineWidths := Grid.GridLineWidth * (Grid.ColCount - 1);
  colWidth := (Grid.ClientWidth div Grid.ColCount) - Grid.GridLineWidth;
  delta := (Grid.ClientWidth - (colWidth * Grid.ColCount)) - lineWidths;
  for c := 0 to Grid.ColCount - 1 do
  begin
    Grid.ColWidths[c] := colWidth;
    if c = Grid.ColCount - 1 then
      Grid.ColWidths[c] := Grid.ColWidths[c] + delta;
  end;
end;

<< Back to main page