?
Solved

Accept only numbers

Posted on 2003-11-06
6
Medium Priority
?
331 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
6 Comments
 
LVL 2

Accepted Solution

by:
Trekker72 earned 140 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 140 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

801 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