whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1548)

Database (90)
Files (137)
Forms (107)
Graphic (116)
IDE (21)
Indy (5)
Internet / LAN (131)
IntraWeb (0)
Math (76)
Misc (127)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (268)
VCL (243)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

33 Visitors Online


 
...validate numeric input in a TEdit?
Autor: Thomas Stutz
[ Print tip ]  

Tip Rating (3):  
     


uses
 
ClipBrd;

procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var
  
Mgs: TMsg;

  procedure EatKey;
  { Clear Keyboardbuffer / Tastaturbuffer leeren }
  
begin
    
PeekMessage(Mgs, 0, WM_CHAR, WM_CHAR, PM_REMOVE);
  end;

begin
  
{ Check if control pressed / überprüfen, ob Control Taste gedrückt }
  
if (ssCtrl in Shift) then
    case 
key of
      
{ Ctrl + v }
      
86:
        begin
          
{ determine if the Clipboard contains a string type }
          { überprüfen, ob es Text in der Zwischenablage hat }
          
if Clipboard.HasFormat(CF_TEXT) then
            
{ Check if text consists of numbers }
            { überprüfen, ob der Text aus Zahlen besteht }
            
try
              
StrToInt(Clipboard.AsText);
            except
              
{ If no then don't insert text }
              { Wenn nein, dann ignoriere das Einfügen }
              
EatKey
            end;
        end;
      { Ctrl + c }
      
67: { do nothing / nichts machen }
        
else
          
EatKey;
    end
  
{ else check for allowed characters such as BackSpace, RETURN...}
  { sonst auf erlaubte Zeichen überprüfen }
  
else if not (Char(Key) in [#8, #13, #46, #48..#57,#96..#105]) then EatKey;
end;

// Use your own Popup Menu to customize paste.

 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners