Delphi-PRAXiS
Seite 1 von 2  1 2      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Lazarus (IDE) (https://www.delphipraxis.net/81-lazarus-ide/)
-   -   Numerische Eingabe in Stinggrid validieren (https://www.delphipraxis.net/182330-numerische-eingabe-stinggrid-validieren.html)

Oniessen 17. Okt 2014 08:58

Numerische Eingabe in Stinggrid validieren
 
Hallo!

Da ich da selbst jetzt 'ne halbe Ewigkeit danach gesucht habe, hier mal eine Lösung.
Unter Lazarus haben die meisten anderen Lösungen leider nicht so richtig funktioniert.
Die ersten beiden Bedingungen waren kein Problem, nur das '-' am Anfang war für mich ziemlich kniffelig (zumindest unter Lazarus).

Vieleicht hilft der Code-Schnipsel ja einem Andern weniger als 2 Tage zu suchen...


Gruß, Oliver

Delphi-Quellcode:
// Validates a Float-only input in a Cell of a TStringGrid
procedure TMyForm.MyGridKeyPress(Sender : TObject; var Key : char);
var
CellText:Widestring;
selStart : integer;
cellRow, cellCol : integer;
begin
  cellRow:= (sender as TStringGrid).Row;
  cellCol:= (sender as TStringGrid).Col;
  CellText:= (sender as TStringGrid).Cells[cellCol,cellRow];
  selStart:= TStringCellEditor(TStringGrid(Sender).Editor).SelStart ; // exessive Typecasting to access SelStart

  if not (Key in [#8, '0'..'9', '-', DecimalSeparator]) then Key := #0
  else if ((Key = DecimalSeparator) or (Key = '-')) and
          (Pos(Key, CellText) > 0)                      then Key := #0
  else if (Key = '-') and (selStart <> 0)               then Key := #0;
end;

Der Volständigkeit halber hier noch die TEdit Lösung (die die meisten sicher schon kennen):

Delphi-Quellcode:
procedure TMyForm.MyEditKeyPress(Sender : TObject; var Key : char);
begin
  if not (Key in [#8, '0'..'9', '-', DecimalSeparator]) then Key := #0
  else if ((Key = DecimalSeparator) or (Key = '-')) and
          (Pos(Key, (Sender as TEdit).Text) > 0)        then Key := #0
  else if (Key = '-') and
          ((Sender as TEdit).SelStart <> 0)             then Key := #0;
end;

freak4fun 17. Okt 2014 11:43

AW: Numerische Eingabe in Stinggrid validieren
 
Find ich gut, dass du das postest.
Ich würde mich über Kommentare im Quelltext sehr freuen. ;)

Der schöne Günther 17. Okt 2014 12:06

AW: Numerische Eingabe in Stinggrid validieren
 
Ganz guter Kandidat wäre z.B. die magische 8. Die sollte man als benannte Konstante rausziehen. Denn dann muss niemand nachschlagen, was denn die 8 in einer ASCII-Tabelle ist (ich musste es grade).

Jumpy 17. Okt 2014 12:34

AW: Numerische Eingabe in Stinggrid validieren
 
Gibts denn in Lazarus nicht wie in Delphi so vordefinierte Konstante ala VK_Back usw?

Oniessen 17. Okt 2014 12:58

AW: Numerische Eingabe in Stinggrid validieren
 
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

DeddyH 17. Okt 2014 13:16

AW: Numerische Eingabe in Stinggrid validieren
 
Zitat:

Zitat von Oniessen (Beitrag 1276402)
Ich denke aber fast, zum Verständniss ist der ASCII-code einfacher...

Wieso sollte der einfacher sein? Bei entsprechend deklarierten Konstanten schlägt man 2 Fliegen mit einer Klappe: der Code wird besser lesbar und man hat bei evtl. Änderungen nur eine einzige zu ändernde Stelle, nämlich die Konstantendeklaration.
Delphi-Quellcode:
procedure TMyForm.MyEditKeyPress(Sender : TObject; var Key : char);
const
  KEY_NONE = #0;
  KEY_BACKSPACE = #8;
begin
  if not (Key in [KEY_BACKSPACE, '0'..'9', '-', DecimalSeparator]) then Key := KEY_NONE
  else if ((Key = DecimalSeparator) or (Key = '-')) and
          (Pos(Key, (Sender as TEdit).Text) > 0) then Key := KEY_NONE
  else if (Key = '-') and
          ((Sender as TEdit).SelStart <> 0) then Key := KEY_NONE;
  // '-' is only allowed as first Character
  // SelStart is the Caret(Text-Curser) Position
end;

Blup 17. Okt 2014 13:46

AW: Numerische Eingabe in Stinggrid validieren
 
Wenn ein Teil des Textes aktuell ausgewählt ist, wird er komplett durch das eingegebene Zeichen ersetzt.
Für Vorzeichen und Dezimalseperator muss man den Text also vor der Prüfung kürzen (SelStart, SelLength).

Oniessen 17. Okt 2014 14:00

AW: Numerische Eingabe in Stinggrid validieren
 
Hallo Blub!

Das würde ich noch nicht mal als "Fehler" ansehen, denn wenn die Zahl oder ein Teil davon markiert/ausgewählt ist, möchte ich es auch mit was anderem ersetzen. ( meine Meinung)

Da wäre doch eher noch an anderer Stelle abzufangen, das z.b. im StringGrid beim weiter schalten zwischen den Zellen NICHT automatisch markiert wird.

Gruß, Oliver

[EDIT]
OK, habs ausprobiert, wenn nur ein Teil markiert ist, geht's nicht mit dem Vorzeichen :-(

insofern hast du recht, das man das evtl. noch anpassen sollte

[/EDIT]

DeddyH 17. Okt 2014 14:00

AW: Numerische Eingabe in Stinggrid validieren
 
Das seh ich nicht so, denn die Zeichen werden ja nicht per Code gesetzt, sondern lediglich ggf. entwertet. Wenn der Anwender also einen Teil des Textes markiert, soll dieser ja wohl mit dem eingegebenen Zeichen überschrieben werden. Oder hab ich Dich jetzt falsch verstanden?

Oniessen 18. Okt 2014 08:03

AW: Numerische Eingabe in Stinggrid validieren
 
Hallo zusammen!

Ich habe die Lösung noch ein wenig angepaßt, so daß jetzt auch die Eingabe von Vorzeichen bzw. DecimalSeparator bei markiertem Text funktioniert.


Erst das TStringGrid:

Delphi-Quellcode:
// Validates a Float-only input in a Cell of a TStringGrid
procedure TMyForm.MyGridKeyPress(Sender : TObject; var Key : char);
const
  KEY_NONE = #0; // empty Char/nullCharacter
  KEY_BACKSPACE = #8;
var
  CellText:Widestring;
  selStart : integer;
  cellRow, cellCol : integer;
  DecSeparator: char;
begin
   DecSeparator:= DefaultFormatSettings.DecimalSeparator; // the StandAlone Symbol "DecimalSeparator" is deprecated


  // If selection is not empty, first delete this selection, then do the rest
  if TStringCellEditor(TStringGrid(Sender).Editor).SelLength > 0 then
     TStringCellEditor(TStringGrid(Sender).Editor).SelText:= '';


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

  // only Keys 0-9(numeric), #8= Backspace, '-' and DecimalSeperator allowed
  if not (Key in [KEY_BACKSPACE, '0'..'9', '-', DecSeparator])
          then Key := KEY_NONE
  // '-' and DecimalSeperator are only allowed once in the String-Input
  else if ((Key = DecSeparator) or (Key = '-')) and (Pos(Key, CellText) > 0)
          then Key := KEY_NONE
  // '-' is only allowed as first Character / SelStart is the Caret(Text-Curser) Position
  else if (Key = '-') and (selStart <> 0)
          then Key := KEY_NONE
end;

und Hier das Tedit:
Delphi-Quellcode:
// Validates a Float-only input in a TEdit
procedure TMyForm.MyEditKeyPress(Sender : TObject; var Key : char);
const
  KEY_NONE = #0;   // empty Char/nullCharacter
  KEY_BACKSPACE = #8;
var
  DecSeparator: Char;
begin
  DecSeparator:= DefaultFormatSettings.DecimalSeparator; // the StandAlone Symbol "DecimalSeparator" is deprecated

  // If selection is not empty, first delete this selection, then do the rest
  if (Sender as TEdit).SelLength > 0 then (Sender as TEdit).SelText:= '';
 

  // only Keys 0-9(numeric), #8= Backspace, '-' and DecimalSeperator allowed
  if not (Key in [KEY_BACKSPACE, '0'..'9', '-', DecSeparator])
          then Key := KEY_NONE
  // '-' and DecimalSeperator are only allowed once in the String-Input
  else if ((Key = DecSeparator) or (Key = '-')) and (Pos(Key, (Sender as TEdit).Text) > 0)
          then Key := KEY_NONE
  // '-' is only allowed as first Character / SelStart is the Caret(Text-Curser) Position
  else if (Key = '-') and ((Sender as TEdit).SelStart <> 0)
          then Key := KEY_NONE
end;
Gruß, Oliver


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

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