Solved

Accept only numbers

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

Industry Leaders: 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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

737 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