Skip to main content
added 227 characters in body
Source Link
Marcodor
  • 5.9k
  • 1
  • 27
  • 26

It's better to put your allow keys in a set as a constant (speed, optimization):

Updated #2 (allowAllow only one decimal char): and handling properly DecimalSeparator.

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char); const Backspace = #8; AllowKeys: set of Char = ['0'..'9', Backspace]; begin  if Key = '.', Backspace]; beginthen Key := DecimalSeparator; if not ((Key in AllowKeys) or ((Key = '.'DecimalSeparator) and (Pos(Key, Edit1.Text) <>= 0)) then begin ShowMessage('Invalid key: ' + Key); Key := #0; end; end; 

For better results take a look at TNumericEdit components included in DevExpress, JVCL, EhLib, RxLib and many other libraries.

It's better to put your allow keys in a set as a constant (speed, optimization):

Updated (allow only one decimal char):

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char); const Backspace = #8; AllowKeys: set of Char = ['0'..'9', '.', Backspace]; begin if not (Key in AllowKeys) or ((Key = '.') and (Pos(Key, Edit1.Text) <> 0)) then begin ShowMessage('Invalid key: ' + Key); Key := #0; end; end; 

It's better to put your allow keys in a set as a constant (speed, optimization):

Updated #2 Allow only one decimal char and handling properly DecimalSeparator.

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char); const Backspace = #8; AllowKeys: set of Char = ['0'..'9', Backspace]; begin  if Key = '.' then Key := DecimalSeparator; if not ((Key in AllowKeys) or (Key = DecimalSeparator) and (Pos(Key, Edit1.Text) = 0)) then begin ShowMessage('Invalid key: ' + Key); Key := #0; end; end; 

For better results take a look at TNumericEdit components included in DevExpress, JVCL, EhLib, RxLib and many other libraries.

Use character literals instead of decimal values
Source Link
Jens Mühlenhoff
  • 14.9k
  • 7
  • 65
  • 114

It's better to put your allow keys in a set as a constant (speed, optimization):

Updated (allow only one decimal char):

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char); const Backspace = #8; AllowKeys: set of Char = [#48['0'..#57'9', #46'.', #8];Backspace]; begin if not (Key in AllowKeys) or ((Key = #46'.') and (Pos(Key, Edit1.Text) <> 0)) then begin ShowMessage('Invalid key: ' + Key); Key := #0; end; end; 

#8 here is your backspace, #48-#57 - digits

It's better to put your allow keys in a set as a constant (speed, optimization):

Updated (allow only one decimal char):

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char); const AllowKeys: set of Char = [#48..#57, #46, #8]; begin if not (Key in AllowKeys) or ((Key = #46) and (Pos(Key, Edit1.Text) <> 0)) then begin ShowMessage('Invalid key: ' + Key); Key := #0; end; end; 

#8 here is your backspace, #48-#57 - digits

It's better to put your allow keys in a set as a constant (speed, optimization):

Updated (allow only one decimal char):

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char); const Backspace = #8; AllowKeys: set of Char = ['0'..'9', '.', Backspace]; begin if not (Key in AllowKeys) or ((Key = '.') and (Pos(Key, Edit1.Text) <> 0)) then begin ShowMessage('Invalid key: ' + Key); Key := #0; end; end; 
added 103 characters in body
Source Link
Marcodor
  • 5.9k
  • 1
  • 27
  • 26

It's better to put your allow keys in a set as a constant (speed, optimization):

Updated (allow only one decimal char):

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char); const AllowKeys: set of Char = [#48..#57, #46, #8]; begin if not (Key in AllowKeys) or ((Key = #46) and (Pos(Key, Edit1.Text) <> 0)) then begin ShowMessage('Invalid key: ' + Key); Key := #0; end; end; 

#8 here is your backspace, #48-#57 - digits

It's better to put your allow keys in a set as a constant (speed, optimization):

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char); const AllowKeys: set of Char = [#48..#57, #46, #8]; begin if not (Key in AllowKeys) then begin ShowMessage('Invalid key: ' + Key); Key := #0; end; end; 

#8 here is your backspace, #48-#57 - digits

It's better to put your allow keys in a set as a constant (speed, optimization):

Updated (allow only one decimal char):

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char); const AllowKeys: set of Char = [#48..#57, #46, #8]; begin if not (Key in AllowKeys) or ((Key = #46) and (Pos(Key, Edit1.Text) <> 0)) then begin ShowMessage('Invalid key: ' + Key); Key := #0; end; end; 

#8 here is your backspace, #48-#57 - digits

Source Link
Marcodor
  • 5.9k
  • 1
  • 27
  • 26
Loading