![]() |
AW: tStringGrid und In-Place-Editor Problem
Herzlichen Dank für eure Antworten. Da mein Urlaub vorbei ist, ist die Zeit für private Projekte mal wieder sehr begrenzt. Aus dem Grund die sporadischen Rückmeldungen...
Ein OnExit für einen In-place-Editor habe ich nicht wirklich funktionierend gelöst bekommen. Aus dem Grund ist der Ansatz jetzt geändert. Die Werte im Editor-Formular werden auch weiterhin nur im Grid gehalten. Nach außen hin stelle ich im OnDraw die entsprechend aufbereiteten Werte dar. Da sicherlich auch andere schon mal auf der Suche nach so etwas waren hier ein kleines Code-Beispiel:
Delphi-Quellcode:
Obiges Beispiel verlangt ein StringGrid, ein Edit-Feld und eine UpDown-Schaltfläche auf dem Formular. Die jeweiligen Events sind zu setzen. Ergebnis ist ein editierbares StringGrid, bei dem Zahlen in jeder Zelle rechtsbündig dargestellt werden und String/fehlerhafte Zahlen linksbündig in rot.
unit uMain;
interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Grids, ComCtrls, StdCtrls; type TForm1 = class (TForm) edValuePrecision: TEdit; UpDown1: TUpDown; StringGrid1: TStringGrid; procedure UpDown1ChangingEx(Sender: TObject; var AllowChange: Boolean; NewValue: SmallInt; Direction: TUpDownDirection); procedure FormCreate(Sender: TObject); procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); private { Private-Deklarationen } fFormatValueString : String; public { Public-Deklarationen } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.FormCreate(Sender: TObject); begin StringGrid1.Options := StringGrid1.Options + [goEditing]; UpDown1.Min := 0; UpDown1.Max := 16; UpDown1.Position := 2; end; Procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); Var aCellString : String; aCellValue : Double; begin aCellString := StringGrid1.Cells [aCol, aRow]; //do we display a data cell? If (aRow >= StringGrid1.FixedRows) and (aCol >= StringGrid1.FixedCols) THen Begin //adjust background colour when cell is selected If gdSelected In State THen Begin StringGrid1.Canvas.Brush.Color := clSkyBlue; StringGrid1.Canvas.FillRect (Rect); end Else Begin StringGrid1.Canvas.Brush.Color := clWhite; StringGrid1.Canvas.FillRect (Rect); end; // does the cell contain a value? If TryStrToFloat (aCellString, aCellValue) THen Begin // if so, display formatted value StringGrid1.Canvas.Font.Color := clBlack; aCellString := Format (form1.fFormatValueString, [aCellValue]); DrawText (StringGrid1.Canvas.Handle, PChar (aCellString), Length (aCellString), Rect, DT_SINGLELINE or DT_RIGHT ); end Else Begin // otherwise display the raw string in a different colour StringGrid1.Canvas.Font.Color := clRed; DrawText (StringGrid1.Canvas.Handle, PChar (aCellString), Length (aCellString), Rect, DT_SINGLELINE or DT_LEFT ); end; Exit; end; // upper left corner for matrix title If (aRow = 0) And (aCol = 0) THen Begin StringGrid1.Canvas.Brush.Color := clMenu; StringGrid1.Canvas.FillRect (Rect); //reduce the grid rectangle to keep the borders Inc (Rect.Left, 2); Dec (Rect.Right, 2); // display the title of the matrix in the upper left corner StringGrid1.Canvas.Font.Color := clRed; DrawText (StringGrid1.Canvas.Handle, PChar (aCellString), Length (aCellString), Rect, DT_SINGLELINE or DT_LEFT ); Exit; end; // do we perform actions in the cells for the column-/row-captions? If (aRow = StringGrid1.FixedRows-1) And (aCol > StringGrid1.FixedCols-1) Or (aRow > StringGrid1.FixedRows-1) And (aCol = StringGrid1.FixedCols-1) THen Begin StringGrid1.Canvas.Brush.Color := cl3dLight; StringGrid1.Canvas.FillRect (Rect); //reduce the grid rectangle to keep the borders Inc (Rect.Left, 2); Dec (Rect.Right, 2); StringGrid1.Canvas.Font.Color := clBlue; Drawtext (StringGrid1.Canvas.Handle, PChar (aCellString), Length (aCellString), Rect, DT_SINGLELINE or DT_LEFT ); end; end; procedure TForm1.UpDown1ChangingEx(Sender: TObject; var AllowChange: Boolean; NewValue: SmallInt; Direction: TUpDownDirection); begin // renew the format string If NewValue >= 0 THen fFormatValueString := '%8.'+IntToStr (NewValue)+'f' Else fFormatValueString := '%8.0f'; // force the stringgrid to update StringGrid1.Invalidate; StringGrid1.Repaint; end; end. Um auf das ausgangsproblem zurückzukommen: In diesem Fall erzeugt das die gewünschte Funktionalität auf eine deutlich einfachere Weise als ursprünglich angedacht. Dennoch kann ich mir einige Fälle vorstellen, in denen ein OnExit für einen In-Place-Editor wünschenswert wäre. @ibp Die ![]() Ansonsten schönen Dank und falls noch jemand was zu ursprünglichen Problem hat..... Jan |
Alle Zeitangaben in WEZ +1. Es ist jetzt 20:24 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