Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
500 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi



Loremo - the 1.5 liter car coming in 2009




Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (0)


TEdit and EConvertError Format this article printer-friendly!Bookmark function is only available for registered users!
Checking TEdit for StrToFloat using OnKeyPress not OnChange
Product:
Delphi all versions
Category:
VCL-General
Skill Level:
Scoring:
Last Update:
09/10/2003
Search Keys:
delphi delphi3000 article borland vcl code-snippet TEdit EConvertError OnKeyPress OnExit Windows-98
Times Scored:
2
Visits:
1939
Uploader: andrew kennaugh
Company: Flow Science
Reference: N/A
 
Question/Problem/Abstract:
When checking for non-numeric characters in a TEdit it might be thought that the OnChange event handler would be OK.  However it doesn't like '-' (minus sign) on its own and can be annoying when it tries to replace what you're typing with a pre-defined replacement.  This is something that's bugged me for a while.  

Instead use the OnKeyPress event handler to detect Chr(13) key and run a checking subroutine that can also be run when the OnExit event occurs if the focus changes.
Answer:



With Edit1 and Button1 on a form use the following code.

The private procedure checkedit(var key: char); is used to check the Edit1.text and replace the value (num) with the previously entered value (lastnum) if it was wrong or to set lastnum to be the new value of num.

The Edit1Exit event handler is to pick up any focus change to another control, if return isn't pressed.

Clearly if the focus doesn't change and isn't pressed then the edit1.text isn't checked.  

HTH

Andy Kennaugh


------------------------------------------------------------------------

unit ECvtu;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    procedure Edit1KeyPress(Sender: TObject; var Key: Char);
    procedure FormCreate(Sender: TObject);
    procedure Edit1Exit(Sender: TObject);
  private
    { Private declarations }
    procedure checkedit(var key : char);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  num, lastnum : real;

implementation

{$R *.DFM}

procedure Tform1.checkedit(var key : Char);
begin
  if (key = chr(13)) then        // if return pressed
  begin
    try
      num := strtofloat(edit1.text);
    Except
      on EConvertError do        // if it's not a number
        begin
          num := lastnum;        // set number to previous value
          edit1.text := floattostr(num);  // put it in edit box
        end;
    end;
      lastnum := num;            // change lastnum, or keep exiting value  
  end;
end;

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  checkedit(key);                // check value
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  lastnum := 0.0;
end;

procedure TForm1.Edit1Exit(Sender: TObject);
var
  key : char;
begin
  key := chr(13);                // 'pretend' return pressed
  checkedit(key);                // check value
end;

end.





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment













 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
E. DSpirito
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)