A TEdit limited to numeric input  

Send By: Arturo_Ortí
Web : N.D.
Email: arturo98@santandersupernet.com
Date: 10/08/00

Tip accessed 88 times

 


Put this code into OnKeyPress event, and your TEdit only will accept numbers:


 procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
 begin
   if ( StrScan('0123456789.-',Key) <> nil ) or
      ( Key = Char(VK_BACK) ) then { BackSpace Key }
   begin
     {Aqui tu tratamiento normal del evento}
     {Here the normal event treatment}
   end
   else
     Key := #0;

 end;





New Tip update, send by Arturo_Ortí at 15/08/99:

Give the bad chance that several decimal points can be entered (".") for what can no longer translate for example you with a StrToFloat.

Here is my solution for this:


 procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
 begin
   if (pos('.',(sender as TEdit).Text)=0) then
   begin
     if not (key in ['0'..'9','.',#8]) then key:=#0;
   end
   else if not (key in ['0'..'9',#8]) then key:=#0;
 end;



With this code, now only is possible enter a single decimal point. And I have add this:


     (sender as TEdit).Text



for if somebody wants to make a loop for all or some the TEdit's of the form:


 procedure TForm1.FormCreate(Sender: TObject);
 var i:integer;
 begin
   for i := ComponentCount-1 downto 0 do
      if Components[i] is TEdit then
            (Components[i] as TEdit).OnKeyPress := Edit1.OnKeyPress;
 end;



For some selected TEdit's: (in this case of the Edit4 at the 8):


 procedure TForm1.FormCreate(Sender: TObject);
 var i,j:integer;
 begin
   for i := ComponentCount-1 downto 0 do
      if Components[i] is TEdit then
        for j:=4 to 8 do
         if (Components[i] as TEdit).Name = 'Edit' + IntToStr(j) then
            (Components[i] as TEdit).OnKeyPress := Edit1.OnKeyPress;
 end;




  • Re-update send by Arturo_Ortí el 16/08/99: (Thanks:)


     procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
     begin
       if key in ['-','.'] then
       case key of
         '-': if (length((sender as TEdit).Text)>0) then
                 if ((pos('-',(sender as TEdit).Text)<>0) or
                     ((sender as TEdit).SelStart >0))
              and
              ((sender as TEdit).SelLength <> length((sender as TEdit).Text))
              then key:=#0;
         '.': if ((pos('.',(sender as TEdit).Text)<>0) and
                  (pos('.',(sender as TEdit).SelText)<>0))
                 xor
                 (pos('.',(sender as TEdit).Text)<>0) or
                 ((length((sender as TEdit).Text)>0) and
                 ((sender as TEdit).Text[length((sender as TEdit).Text)] = '-')) or
                 ((sender as TEdit).SelLength = length((sender as TEdit).Text)) or
                 ((sender as TEdit).SelStart = 0)
              then key:=#0;
       end
       else if not (key in ['0'..'9',#8]) then key:=#0;
     end;
    



    With this update, the minus sign '-' else works for enter negative numbers.

    According to the author's words:
    Notices that they should have the decimals configured with point and not with coma in the regional configuration of the güindows, if not, when they try to translate with StrToFloat it will give error.


    Other more elegant method:


     procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
     begin
       if Key <> Char(VK_BACK) then
         try
           StrToFloat(Edit1.Text+Key+'0');
         except
           Key:=#0
         end;
     end;
    




    Updated at 06/11/99

    This never ends...
    Arturo, send us two new methods that works when the text is pasted from the ClipBoard:


    For Integer Numbers:




     procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
     var sNumAnterior:string;
     begin
       sNumAnterior := (sender as TEdit).Text;
       if (key <> #3) and (key <> #22) then  // #3 = Copiar (Ctrl + C)
       begin                                 // #22 = Pegar (Ctrl + V)
         if key = '-' then
         begin
           if ((pos(key,(sender as TEdit).Text) > 0) and
              (pos(key,(sender as TEdit).SelText) = 0))
              or
              ((sender as TEdit).SelStart > 0)
           then key:=#0;
         end
         else if not (key in ['0'..'9',#8]) then key:=#0;
       end;
       if key = #22 then
       try
         key := #0;
         (sender as TEdit).PasteFromClipBoard;
         StrToInt((sender as TEdit).Text);
       except
         (sender as TEdit).Text := sNumAnterior;
         (sender as TEdit).SelStart := Length((sender as TEdit).Text);
       end;
     end;
    




    For Real Numbers:


     procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
     var sNumAnterior:string;
     begin
       sNumAnterior := (sender as TEdit).Text;
       if (key <> #3) and (key <> #22) then  // #3 = Copiar (Ctrl + C)
       begin                                 // #22 = Pegar (Ctrl + V)
         if key in ['-','.'] then
         begin
           if (pos(key,(sender as TEdit).Text) > 0) and
              (pos(key,(sender as TEdit).SelText) = 0)
              then key:=#0;
           case key of
             '-': if ((sender as TEdit).SelStart > 0) then key:=#0;
             '.': if ((sender as TEdit).SelStart = 0) or
                     (((sender as TEdit).SelStart = 1) and
                     ((sender as TEdit).Text[1] = '-'))
                  then key:=#0;
           end;
         end
         else if not (key in ['0'..'9',#8]) then key:=#0;
       end;
       if key = #22 then
       try
         key := #0;
         (sender as TEdit).PasteFromClipBoard;
         StrToFloat((sender as TEdit).Text);
       except
         (sender as TEdit).Text := sNumAnterior;
         (sender as TEdit).SelStart := Length((sender as TEdit).Text);
       end;
     end;
    




    Sent by: Alicia V. (loly6@hotmail.com)
    In the case of severals TEdits, it's possible use the same event for one TEdit, and assign it to the others TEdits:


     procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
     Var
        editor : TEdit;
     begin
          editor := Sender As TEdit;
          if (key <> Char(VK_BACK)) Then
             try
                StrToFloat(Editor.Text+Key+'0');
             except
                   Key := #0
             end;
     end;
    




    Updated at 27/05/2000