Delphi-PRAXiS
Seite 2 von 2     12   

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Algorithmen, Datenstrukturen und Klassendesign (https://www.delphipraxis.net/78-algorithmen-datenstrukturen-und-klassendesign/)
-   -   Delphi tStringGrid und In-Place-Editor Problem (https://www.delphipraxis.net/153410-tstringgrid-und-place-editor-problem.html)

BoolString 18. Aug 2010 13:16

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:
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.
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.

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 Schweizer ComboBox Version habe ich versucht in meinem Projekt mit umzusetzen. Allerdings wird hier OnSelectCell verwendet. Dies beißt sich leider etwas mit meinem Dateneditor. Wenn der 'In-Place' Editor in Form eines tEdit noch aktiv ist und eine gleichzeitig eine Selection angelegt wird oder der Nutzer Spalten/Zeilen verschiebt, dann bleiben an flascher Stelle leider teilweise die werte stehen...

Ansonsten schönen Dank und falls noch jemand was zu ursprünglichen Problem hat.....

Jan


Alle Zeitangaben in WEZ +1. Es ist jetzt 05:23 Uhr.
Seite 2 von 2     12   

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