Einzelnen Beitrag anzeigen

Oniessen

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

AW: Numerische Eingabe in Stinggrid validieren

  Alt 17. Okt 2014, 12:58
Hallo nochmal!

Es ging mir ja eigentlich nur um die Besonderheit im TStringGrid. Die andere TEdit-Lösung findet sich recht schnell durch g**geln... und ich dachte eigentlich, das wäre schon recht klar soweit...

Sicher gibts auch Konstanten in Lazarus. Ich denke aber fast, zum Verständniss ist der ASCII-code einfacher...

Naja, hier noch mal die TEdit-Version mit Kommentaren:

Delphi-Quellcode:
procedure TMyForm.MyEditKeyPress(Sender : TObject; var Key : char);
begin
  if not (Key in [#8, '0'..'9', '-', DecimalSeparator]) then Key := #0 // #0 = empty Char/nullCharacter
  // only Keys 0-9(numeric), #8= Backspace, '-' and DecimalSeperator allowed
  else if ((Key = DecimalSeparator) or (Key = '-')) and
          (Pos(Key, (Sender as TEdit).Text) > 0) then Key := #0 // #0 = empty Char/nullCharacter
  // '-' and DecimalSeperator are only allowed once in the String-Input
  else if (Key = '-') and
          ((Sender as TEdit).SelStart <> 0) then Key := #0; // #0 = empty Char/nullCharacter
  // '-' is only allowed as first Character
  // SelStart is the Caret(Text-Curser) Position
end;

Den Typecast zur Veranschaulichung mal anders geschrieben:
(ungetestet)

Delphi-Quellcode:
.
..
selStart:= TStringCellEditor(TStringGrid(Sender).Editor).SelStart ;
// (((Sender as TStringGrid).Editor) as TStringCellEditor).SelStart
..
.

Gruß, Oliver
  Mit Zitat antworten Zitat