Skip to content

Instantly share code, notes, and snippets.

@jpluimers
Last active October 27, 2016 11:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpluimers/cae755c71a94b755fc13e8a7361b9b84 to your computer and use it in GitHub Desktop.
Save jpluimers/cae755c71a94b755fc13e8a7361b9b84 to your computer and use it in GitHub Desktop.
{== NumberEdits =======================================================}
{: This unit implements an edit control for the input of positive
integer numbers.
@author Dr. Peter Below
@desc Version 1.0 created 15 Oktober 2001<BR>
Current revision 1.0<BR>
Last modified 15 Oktober 2001<P>
This unit started life as an example component for the Borland newsgroups. }
{======================================================================}
{$BOOLEVAL OFF}{Unit depends on shortcut boolean evaluation}
Unit NumberEdits;
Interface
Uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
Type
TEMreplaceSel = TWMSettext;
TFilteredEdit = Class( TEdit )
Private
Procedure WMPaste( Var msg: TMessage ); message WM_PASTE;
Procedure WMSetText( Var msg: TWMSettext ); message WM_SETTEXT;
Procedure EMReplaceSel( Var msg: TEMReplaceSel );
message EM_REPLACESEL;
Protected
Procedure KeyPress(Var Key: Char); override;
Function IsCharValid( ch: Char ): Boolean; virtual; abstract;
Function IsStringValid( Const S: String ): Boolean; virtual;
End;
TNumberedit = Class(TFilteredEdit)
Protected
Function IsCharValid( ch: Char ): Boolean; override;
End;
Procedure Register;
Implementation
Uses clipbrd;
Procedure Register;
Begin
RegisterComponents('PBGoodies', [tNumberedit]);
End;
Procedure TFilteredEdit.EMReplaceSel(Var msg: TEMReplaceSel);
Begin
If IsStringValid( msg.Text ) Then
inherited;
End;
Function TFilteredEdit.IsStringValid(Const S: String): Boolean;
Var
i: Integer;
Begin
Result := True;
For i:= 1 To Length(S) Do
If not IsCharValid(S[i]) Then Begin
Result := False;
Break;
End;
End;
Procedure TFilteredEdit.KeyPress(Var Key: Char);
Begin
If IsCharValid(Key) or (Key In [#8, ^V, ^C, ^X]) Then
// #8 = backspace, ^V = Ctrl-V, ^C = Ctrl-C, ^X = Ctrl-X
inherited
Else
Key := #0;
End;
Procedure TFilteredEdit.WMPaste(Var msg: TMessage);
Begin
If IsStringValid( Clipboard.AsText ) Then
inherited;
End;
Procedure TFilteredEdit.WMSetText(Var msg: TWMSettext);
Begin
If IsStringValid( msg.Text ) Then
inherited;
End;
Function tNumberedit.IsCharValid(ch: Char): Boolean;
Begin
result := ch In ['0'..'9'];
End;
End.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment