![]() |
Breite von StringGrid dem Textinhalt anpassen macht Probleme
Hallo zusammen,
ich habe eine Prozedur, die die Spaltenbreite eines StringGrids dem Textinhalt anpassen soll. Das funktioniert soweit auch wunderbar, nur bei Kommazahlen treten Fehler auf. Nach dem Komma wird nämlich schon die Spalte beendet. Hier ist einmal die Prozedur. Vielleicht sieht ja jemand wo das Problem liegt.
Delphi-Quellcode:
Also zusammenfassend:
procedure ActSizeSG(var MySG:TStringGrid);
var colIndex, rowIndex :Integer; myColWidth:word; height:word; begin // gehe jede Spalte durch for colIndex := 0 to MySG.ColCount - 1 do begin // setzte fuer jede Spalte am Anfang die Hoehe 0 height:=0; // setze am Anfang die Standardbreite MyColWidth := MySG.DefaultColWidth; // gehe dann jede Reihe der SPalte durch for rowIndex := 0 to MySG.RowCount - 1 do begin // wenn die Standardbreite kleiner der Textbriete in der jeweiligen Zelle ist if MyColWidth < MySG.Canvas.TextWidth(MySG.Cells[colIndex, rowIndex]) then begin // dann setze die Breite auf Textbreite plus einen Abstand von 10 MyColWidth := MySG.Canvas.TextWidth(MySG.Cells[colIndex, rowIndex]) + 10; // setze die neue Zellenbreite MySG.ColWidths[colIndex] := MyColWidth; end; // ermittle durchgehend die Hoehe des gesamten StirngGrids height:=height+CDefaultRowHeight; end; end; // zaehle auch die letzte Reihe dazu MySG.Height:=height+CDefaultRowHeight; end; Warum zählt Canvas alles hinter dem Komma nicht mehr mit? |
Re: Breite von StringGrid dem Textinhalt anpassen macht Prob
Also das geht bei mir:
Delphi-Quellcode:
function ResizeStringGridColumns(const s: String; st: TStringGrid): Cardinal;
begin result := st.Canvas.TextWidth(s)+10; end; procedure TForm1.Button1Click(Sender: TObject); var s: String; begin s := '16518451897645489784987984878974,65465465'; StringGrid1.DefaultColWidth := ResizeStringGridColumns(s, StringGrid1); StringGrid1.Cells[1,1] := s; end; |
Alle Zeitangaben in WEZ +1. Es ist jetzt 21:17 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 by Thomas Breitkreuz