Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    28
    Rep Power
    0

    Edit control only for positive values ?


    I'm using an Edit control in my form. It must accept only the integer values not any other symbols like [-,$,etc].
    Is there any property to make it possible ?

    Thanks in advance
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2006
    Location
    Carlsbad, CA
    Posts
    2,057
    Rep Power
    383
    I don't think so (I use TadvEdit from TMS so I can not remember off hand).
    I believe you have to add your own code to the onChange event which should be pretty easy.

    Clive
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    151
    Rep Power
    3
    Originally Posted by Murugavel
    I'm using an Edit control in my form. It must accept only the integer values not any other symbols like [-,$,etc].
    Is there any property to make it possible ?

    Thanks in advance
    In newer Delphi versions this property is available. I can't remember since which, but TEdit in Delphi XE2 (and XE3) definitely has NumbersOnly property.

    If your Delphi version does not support this, you still can do it easily. Do the following in the hosting form's OnCreate event (the code assumes that your TEdit name is Edit1).

    Code:
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      ..
      SetWindowLong(Edit1.Handle, GWL_STYLE, GetWindowLong(Edit1.Handle, GWL_STYLE) or ES_NUMBER)
      ..
    end;
    [END]
    For more information regarding the ES_NUMBER style, consult your Windows SDK for information about CreateWindow.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2006
    Location
    Carlsbad, CA
    Posts
    2,057
    Rep Power
    383
    ES_NUMBER
    One thing to watch with this approach is that (according to documentation) it will
    not prevent the user from pasting non-numeric data into the control.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    151
    Rep Power
    3
    Originally Posted by clivew
    One thing to watch with this approach is that (according to documentation) it will
    not prevent the user from pasting non-numeric data into the control.
    And your solution is... ?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2006
    Location
    Carlsbad, CA
    Posts
    2,057
    Rep Power
    383
    And your solution is... ?
    Using the onChange event, as I mentioned previously.
    BTW: If using a Delphi version that supports the NumbersOnly property, it still allows pasting of non-numeric data.
    Of course, this might be an issue that Murugavel can live with or could be handled
    satisfactorily in the onExit event. We don't know.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    354
    Rep Power
    7
    I think this article by Peter Below describes the issues involved with creating a Numeric Edit Control. Very informative...
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    151
    Rep Power
    3
    ^
    Do you realize that OnChange only be triggered after the edit control content got changed? So, users still can paste non-numeric characters. Do you suggest to do cleaning up in the OnChange handler?

    If so, what is the advantage of using OnChange in this case? Users still be able to paste non-numeric characters?

    The problem with ES_NUMBER still allowing users to paste non-numeric chars was actually a bug with Windows. In Windows Vista and 7 you can't paste non-numeric chars in any TEdit with ES_NUMBER style.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    25
    Rep Power
    0

    Numbers only


    Best solution I found ever:
    procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
    begin
    if not( Key in ['0'..'9'] +[#13,#8])
    then
    Key := #0;
    end;

    Does not allow cut/paste or unwanted numbers or whatever you dont want
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    354
    Rep Power
    7
    Best solution I found ever:
    procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
    begin
    if not( Key in ['0'..'9'] +[#13,#8])
    then
    Key := #0;
    end;

    Does not allow cut/paste or unwanted numbers or whatever you dont want
    Right clicking and selecting paste will allow non-numeric data to be pasted into the control. I've used this before, but it just doesn't cover all the possibilities.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    354
    Rep Power
    7
    Originally Posted by Luthfi
    ^
    Do you realize that OnChange only be triggered after the edit control content got changed? So, users still can paste non-numeric characters. Do you suggest to do cleaning up in the OnChange handler?

    If so, what is the advantage of using OnChange in this case? Users still be able to paste non-numeric characters?

    The problem with ES_NUMBER still allowing users to paste non-numeric chars was actually a bug with Windows. In Windows Vista and 7 you can't paste non-numeric chars in any TEdit with ES_NUMBER style.
    Microsoft documentation still shows that ES_NUMBER allows non-numeric data to be pasted into the control..

    http://msdn.microsoft.com/en-us/library/windows/desktop/bb775464%28v=vs.85%29.aspx
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    151
    Rep Power
    3
    Originally Posted by majlumbo
    Microsoft documentation still shows that ES_NUMBER allows non-numeric data to be pasted into the control..

    http://msdn.microsoft.com/en-us/library/windows/desktop/bb775464%28v=vs.85%29.aspx
    In reality in Windows Vista and 7 that would not be allowed anymore. Why fix something that was not a bug/problem? Do you know in original documentation that exception was not shown anywhere? Consult Windows SDK documentation came with Delphi 7 or before it.
    Last edited by Luthfi; October 10th, 2012 at 03:43 PM.
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    25
    Rep Power
    0

    Cut/Paste


    Correct, right clicking does allow a paste, but that is about all. It will never allow any further steps. The pasted data will simply just stay there and do nothing. I have been using this code extensively with zero fail rate for at least 10 years. I consider this code to cover all possibilities. And if it is so important that you must not even see pasted data, simply deactivate right-click on that component.
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2006
    Location
    Carlsbad, CA
    Posts
    2,057
    Rep Power
    383
    Lots of good advice. It does depend a little on how rigid are the requirements.
    I have used the TMS component TadvEdit for years which handles all these issues
    and more through properties and methods; but, of course, it is not free.

    Clive
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    151
    Rep Power
    3
    Originally Posted by sihaam
    Correct, right clicking does allow a paste, but that is about all. It will never allow any further steps. The pasted data will simply just stay there and do nothing. I have been using this code extensively with zero fail rate for at least 10 years. I consider this code to cover all possibilities. And if it is so important that you must not even see pasted data, simply deactivate right-click on that component.
    For this, I disagree. We should not make it more difficult for users. Therefore we should not prevent them from copy-pasting. I do agree that handling OnKeyPress is a good way (in Windows versions that have ES_NUMBER pasting bug, and also for cross platform intention). But you must accompany it with handling WM_PASTE message (when the program is running in Windows versions that have ES_NUMBER pasting bug) to make it perfect. When you get this message, analyze the current content of the keyboard for possible violation and only do real pasting when there is no violation.

    Of course this need to be implemented in new TEdit descendant class. Not really easy for beginners. That is why I kept this solution and offer easier solution which run perfectly, at least in Windows Vista and 7.

    @murugravel:
    If you decide to code your own TEdit descendant class, do not use OnKeyPress. Override its KeyPress method instead.

    Btw, in my own TEdit descendant class you can customize the rule. Not only you can limit to accepting numeric chars, but you can also limit to accept certain set of chars.

    Originally Posted by clieview
    Lots of good advice. It does depend a little on how rigid are the requirements.
    I have used the TMS component TadvEdit for years which handles all these issues
    and more through properties and methods; but, of course, it is not free.
    Actually writing your own TEdit descendant class for this case is not really difficult. So you can get it for free. Just detect if you are not in Delphi XE2 (for example) and then create similar properties. That's for compile time, so encapsulate related codes within proper compiler directives.

    In runtime your code should detect if it's running under Vista or 7. When it's running in older Windows, do the OnKeyPress and WM_PASTE handling.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo