Einzelnen Beitrag anzeigen

Oniessen

Registriert seit: 17. Feb 2010
18 Beiträge
 
#4

AW: Float und Integer in TDBGrid validieren

  Alt 6. Mär 2018, 09:37
Hallo!

Ich habe jetzt eine "halbe Lösung"

Folgendes funktioniert schon mal für das minus und alle anderen Zeichen:

Delphi-Quellcode:
procedure TMyForm.MyDBGridKeyPress(Sender: TObject; var Key: char);
  var
  CellText:Widestring;
  selStart : integer;
  cellRow, cellCol : integer;
  MyDecSep : Char;
  MyGrid : TStringGrid;
  begin
    // First some exessive Typecasting to access the position
    MyGrid := TStringGrid(Sender as TDBGrid);
    MyDecSep:= DefaultFormatSettings.DecimalSeparator;

    if TStringCellEditor(MyGrid.Editor).SelLength > 0 then
       TStringCellEditor(MyGrid.Editor).SelText:= '';

    cellRow:= (MyGrid).Row;
    cellCol:= (MyGrid).Col;
    CellText:= (MyGrid).Cells[cellCol,cellRow];
    // exessive Typecasting to access SelStart
    selStart:= TStringCellEditor(MyGrid.Editor).SelStart ;

    // #8 = Backspace; #0 = empty key ;
    if not (Key in [#8, '0'..'9', '-', MyDecSep]) then Key := #0

    else if ((Key = MyDecSep) or (Key = '-'))
             and (Pos(Key, CellText) > 0) then Key := #0

    else if (Key = '-') and (selStart <> 0) then Key := #0;
  end;
Nur leider nicht für das Komma

Das lässt sich immer noch mehrfach eintippen...
Ich hab es auch schon mit ',' oder #44 versucht, aber irgendwie will das nicht funktionieren

Irgendeine Idee??

Gruß, Oliver


PS:
So komme ich übrigens doch an den DataType
Delphi-Quellcode:
...
 if (sender as TDBGrid).SelectedColumn.Field.DataType = ftfloat then
...

Geändert von Oniessen ( 6. Mär 2018 um 09:43 Uhr) Grund: PS hinzu
  Mit Zitat antworten Zitat