[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Accept only numbers

Posted on 2003-11-06
6
Medium Priority
?
332 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

650 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