Delphi-PRAXiS

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)

daywalker-dj-k 5. Mär 2004 11:58


"Optimale Breite" bei StringGrid einstellbar?
 
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

Robert_G 5. Mär 2004 12:12

Re: "Optimale Breite" bei StringGrid einstellbar?
 
Wir haben hier doch eine Supi-Suchfunktion...

Einfach mal nach Hier im Forum suchenstringgrid AND breite suchen. :wink:

Kommt unter anderem das dabei raus:
http://www.delphipraxis.net/internal...?p=23699#23699

Etwas übersichtlicher wäre es wohl so:
Delphi-Quellcode:
Var
  vCol, vRow, Len                      : Integer,
Begin
  ...
  For vCol := FixedCols To pred(Grid.ColCount) Do
  Begin
    len := 20; // min. Breite
    For vRow := FixedRows To pred(Grid.RowCount) Do
      If len < Grid.Canvas.TextWidth(Grid.Cells[vRow, vCol]) Then
        len := Grid.Canvas.TextWidth(Grid.Cells[vRow, vCol]);
    Grid.ColWidths[vCol] := len;
  End;
  ...
End;

himitsu 5. Mär 2004 12:15

Re: "Optimale Breite" bei StringGrid einstellbar?
 
Ich würde nicht nur
Delphi-Quellcode:
Grid.Canvas.TextWidth(Grid.Cells[vRow, vCol])
werwenden, sondern z.B.
Delphi-Quellcode:
Grid.Canvas.TextWidth(Grid.Cells[vRow, vCol]) + 4
Delphi-Quellcode:
  For vCol := FixedCols To pred(Grid.ColCount) Do
  Begin
    len := 20; // min. Breite
    For vRow := FixedRows To pred(Grid.RowCount) Do
      If len < Grid.Canvas.TextWidth(Grid.Cells[vRow, vCol]) + 4 Then
        len := Grid.Canvas.TextWidth(Grid.Cells[vRow, vCol]) + 4;
    Grid.ColWidths[vCol] := len;
  End;
Den um die Schrift ist ja noch ein gewisser Rahmen, den sollte man nicht vergessen.

Robert_G 5. Mär 2004 12:20

Re: "Optimale Breite" bei StringGrid einstellbar?
 
:oops: hatte nur den Code kopiert und Col/Row in vCol/vRow geäandert.
Richtiger wäre
Delphi-Quellcode:
  ...
  With Grid Do
    For vCol := FixedCols To pred(ColCount) Do
    Begin
      len := 20; // min. Breite
      For vRow := FixedRows To pred(RowCount) Do
        If len < Canvas.TextWidth(Cells[vRow, vCol]) + GridLineWidth + 4 Then
          len := Canvas.TextWidth(Cells[vRow, vCol]) + GridLineWidth + 4;
      ColWidths[vCol] := len;
    End;
  ...

daywalker-dj-k 5. Mär 2004 12:44

Re: "Optimale Breite" bei StringGrid einstellbar?
 
hallo,
vielen dank erstmal für die hilfe. nur leider klappt das bei mir nicht :-)

also:

Delphi-Quellcode:
function TForm1.OptStringGridBreite(Sender: TObject): string;
var vCol, vRow, len: integer;
begin
  with TStringGrid(Sender) do
  for vCol := FixedCols to pred(ColCount) do
    begin
      len := 1; // min. Breite
      for vRow := FixedRows to pred(RowCount) do
        if len < Canvas.TextWidth(Cells[vRow, vCol]) + GridLineWidth + 4 then
          len := Canvas.TextWidth(Cells[vRow, vCol]) + GridLineWidth + 4;
      ColWidths[vCol] := len;
    end;
end;
aufgerufen wird es so:

Delphi-Quellcode:
OptStringGridBreite(StringGrid1);
kurz bevor die funktion aufgerufen wird, wird erst die anzahl der spalten festgelegt:

Delphi-Quellcode:
StringGrid1.ColCount := ComboBox5.Items.Capacity;
zum einen wird eine spalte mit daten (z.b. 24.01.2004) zu eng gerückt und zum anderen werden viele spalten nicht eng genug gerückt (z.b. leere spalten. gibt es noch einen mindestbreitenwert von delphi aus? ich habe den im qt ja schona uf 1 begrenzt.

danke
mfg
tkliewe

Robert_G 11. Mär 2004 23:41

Re: "Optimale Breite" bei StringGrid einstellbar?
 
:wall: :wall: :wall:
Tausche mal vRow und vCol um...

joehd 17. Jul 2019 08:46

AW: "Optimale Breite" bei StringGrid einstellbar?
 
So ist es jetzt dann richtig würde ich sagen zumindest bei mir geht es so ...
Ob eine Spalte gleich 0 wird habe ich nicht probiert
Delphi-Quellcode:
function TForm1.OptStringGridBreite(Sender: TObject): string;
var vCol, vRow, len: integer;
begin
  with TStringGrid(Sender) do
  for vCol := FixedCols to pred(ColCount) do
    begin
      len := 1; // min. Breite
      for vRow := FixedRows to pred(RowCount) do
        if len < Canvas.TextWidth(Cells[vcol, vrow]) + GridLineWidth + 4 then
          len := Canvas.TextWidth(Cells[vcol, vrow]) + GridLineWidth + 4;
      ColWidths[vCol] := len;
    end;
end;

dominicday 9. Aug 2019 04:26

AW: "Optimale Breite" bei StringGrid einstellbar?
 
vielen dank für deine antwort. nur leider klappt das bei mir nicht :cry:

hoika 9. Aug 2019 06:59

AW: "Optimale Breite" bei StringGrid einstellbar?
 
Hallo,
"klappt nicht" ist keine Fehlermeldung ...

Wann genau rufst du die Methode auf?
Und poste noch mal deine Methode.

dummzeuch 9. Aug 2019 08:09

AW: "Optimale Breite" bei StringGrid einstellbar?
 
///<summary>
/// Resizes the columns of a TCustomGrid to fit their contents
/// @param Grid is the TCustomGrid to work on
/// @param Options is a TResizeOptionSet specifying additional options,
/// defaults to an empty set.
/// @param RowOffset gives the first row to use, -1 means "start at the first non-fixed row"
/// @param ConstantCols is an array containg the indexes of columns that should keep their
/// width.
/// @returns True, if the grid is not wide enough to display all columns (that is: It should
/// have a horizontal scrollbar)
/// False, if all columns fit without scrolling
/// @note that the default is to use the first 10 rows. </summary>
function TGrid_Resize(_Grid: TCustomGrid): Boolean; overload;
function TGrid_Resize(_Grid: TCustomGrid; _Options: TResizeOptionSet; _RowOffset: Integer = -1): Boolean; overload;
function TGrid_Resize(_Grid: TCustomGrid; _Options: TResizeOptionSet;
const _ConstantCols: array of Integer; _RowOffset: Integer = -1): Boolean; overload;

https://osdn.net/projects/dzlib-tool...lib-tools#l116

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 07:39 Uhr.

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