Solved

Accept only numbers

Posted on 2003-11-06
6
324 Views
Last Modified: 2010-04-05
Hi, I was just wondering if there was an easy way to accept only numbers instead of a string in a TEdit or TMemo, etc? Thank you.
0
Comment
Question by:ionet
  • 2
6 Comments
 
LVL 2

Accepted Solution

by:
Trekker72 earned 35 total points
ID: 9699016
try using this in the OnKeyPress event

if not (Key in [#8, '0'..'9']) then
    Key := #0;

hope that helps,
  terry
0
 

Author Comment

by:ionet
ID: 9703758
Yes, see I already know that method I just have no idea what it means. Can someone please explain it?
0
 
LVL 2

Expert Comment

by:Trekker72
ID: 9706090
from Delphi help...

TWinControl.OnKeyPress
Occurs when key pressed.
type TKeyPressEvent = procedure (Sender: TObject; var Key: Char) of object;
property OnKeyPress: TKeyPressEvent;

Description
Use the OnKeyPress event handler to make something happen as a result of a single character key press.
The Key parameter in the OnKeyPress event handler is of type Char; therefore, the OnKeyPress event registers the ASCII character of the key pressed.

...so basically when the edit box has the focus and a key is pressed the Char value is passed to the event.
The code i posted earlier checks to see if the key pressed was a numeral ('0'..'9') or backspace (#8) and if not set it to null (#0) so it does not get put into the edit box.  see www.asciitable.com for some ascii conversions
0
 
LVL 3

Assisted Solution

by:ThoseBug
ThoseBug earned 35 total points
ID: 9718522
to numbers..integer

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 = Copy (Ctrl + C)
  begin                                 // #22 = paste (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;

to float 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 = Copy (Ctrl + C)
  begin                                 // #22 = Paste (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;
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now