Einzelnen Beitrag anzeigen

peterbelow

Registriert seit: 12. Jan 2019
Ort: Hessen
672 Beiträge
 
Delphi 11 Alexandria
 
#11

AW: "Optimale Breite" bei StringGrid einstellbar?

  Alt 9. Aug 2019, 13:32
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.
Peter Below

Geändert von peterbelow ( 9. Aug 2019 um 13:34 Uhr)
  Mit Zitat antworten Zitat