Solved

Accept only numbers

Posted on 2003-11-06
6
330 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 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

690 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