![]() |
"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 |
Re: "Optimale Breite" bei StringGrid einstellbar?
Wir haben hier doch eine Supi-Suchfunktion...
Einfach mal nach ![]() Kommt unter anderem das dabei raus: ![]() 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; |
Re: "Optimale Breite" bei StringGrid einstellbar?
Ich würde nicht nur
Delphi-Quellcode:
werwenden, sondern z.B.
Grid.Canvas.TextWidth(Grid.Cells[vRow, vCol])
Delphi-Quellcode:
Grid.Canvas.TextWidth(Grid.Cells[vRow, vCol]) + 4
Delphi-Quellcode:
Den um die Schrift ist ja noch ein gewisser Rahmen, den sollte man nicht vergessen.
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; |
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; ... |
Re: "Optimale Breite" bei StringGrid einstellbar?
hallo,
vielen dank erstmal für die hilfe. nur leider klappt das bei mir nicht :-) also:
Delphi-Quellcode:
aufgerufen wird es so:
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;
Delphi-Quellcode:
kurz bevor die funktion aufgerufen wird, wird erst die anzahl der spalten festgelegt:
OptStringGridBreite(StringGrid1);
Delphi-Quellcode:
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.
StringGrid1.ColCount := ComboBox5.Items.Capacity;
danke mfg tkliewe |
Re: "Optimale Breite" bei StringGrid einstellbar?
:wall: :wall: :wall:
Tausche mal vRow und vCol um... |
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; |
AW: "Optimale Breite" bei StringGrid einstellbar?
vielen dank für deine antwort. nur leider klappt das bei mir nicht :cry:
|
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. |
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; ![]() |
Alle Zeitangaben in WEZ +1. Es ist jetzt 13:41 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz