Delphi-PRAXiS
Seite 2 von 2     12   

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   GUI-Design mit VCL / FireMonkey / Common Controls (https://www.delphipraxis.net/18-gui-design-mit-vcl-firemonkey-common-controls/)
-   -   Delphi "Optimale Breite" bei StringGrid einstellbar? (https://www.delphipraxis.net/17422-optimale-breite-bei-stringgrid-einstellbar.html)

peterbelow 9. Aug 2019 13:32

AW: "Optimale Breite" bei StringGrid einstellbar?
 
Zitat:

Zitat von daywalker-dj-k (Beitrag 126276)
hallo,
kann man irgendwo einstellen, dass das stringgrid die optimale breite nutzen soll (also quasi wie z.b. bei excel). die breite soll sich automatisch so einstellen, das der länste text so gerade darstellbar ist.

oder muss man das von hand programmieren?

danke
mfg
tkliewe

Delphi-Quellcode:
{! 
<summary>
 SetGridColumnWidths sizes the columns of a grid to fit their content.</summary>
<param name="Grid">
 is the grid to modify. This parameter cannot be nil.</param>
<param name="Columns">
 specifies the columns to resize. If an empty array is passed for Columns
 all columns are sized, otherwise only those whos index is in Columns
 are sized. </param>
}
procedure SetGridColumnWidths(Grid: TStringGrid;
  const Columns: array of Integer);

procedure SetGridColumnWidths(Grid: TStringGrid;
  const Columns: array of Integer);

  function ColInArray(n: Integer): Boolean;
  var
    i: Integer;
  begin
    if Length(Columns) = 0 then
      Result := true
    else begin
      Result := false;
      for i := Low(Columns) to High(Columns) do
        if n = Columns[i] then begin
          Result := true;
          Break;
        end; { If }
    end; { Else }
  end;

const
  DEFBORDER = 8;
var
  max, temp, i, n: Integer;
begin
  Assert(Assigned(Grid));
  Grid.Canvas.Font := Grid.Font;
  for n := 0 to Grid.Colcount - 1 do
    if ColInArray(n) then begin
      max := 0;
      for i := 0 to Grid.RowCount - 1 do begin
        temp := Grid.Canvas.TextWidth(Grid.Cells[n, i]) + DEFBORDER;
        if temp > max then
          max := temp;
      end; { For }
      if max > 0 then begin
        Grid.ColWidths[n] := Min(max, Grid.ClientWidth * 9 div 10);
      end;
    end; { If }
end; {SetGridColumnWidths }
Benötigt system.math in der Uses-Klausel.


Alle Zeitangaben in WEZ +1. Es ist jetzt 17:00 Uhr.
Seite 2 von 2     12   

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz