October 9th, 2012, 12:05 PM
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
October 9th, 2012, 04:45 PM
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.
October 10th, 2012, 12:20 PM
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.
Originally Posted by Murugavel
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).
For more information regarding the ES_NUMBER style, consult your Windows SDK for information about CreateWindow.
procedure TForm1.FormCreate(Sender: TObject);
SetWindowLong(Edit1.Handle, GWL_STYLE, GetWindowLong(Edit1.Handle, GWL_STYLE) or ES_NUMBER)
October 10th, 2012, 01:19 PM
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.
October 10th, 2012, 02:02 PM
And your solution is... ?
Originally Posted by clivew
October 10th, 2012, 02:38 PM
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.
October 10th, 2012, 02:50 PM
I think this article by Peter Below describes the issues involved with creating a Numeric Edit Control. Very informative...
October 10th, 2012, 02:53 PM
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.
October 10th, 2012, 03:38 PM
Best solution I found ever:
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
if not( Key in ['0'..'9'] +[#13,#8])
Key := #0;
Does not allow cut/paste or unwanted numbers or whatever you dont want
October 10th, 2012, 04:23 PM
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.
October 10th, 2012, 04:28 PM
Microsoft documentation still shows that ES_NUMBER allows non-numeric data to be pasted into the control..
Originally Posted by Luthfi
October 10th, 2012, 04:41 PM
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.
Originally Posted by majlumbo
Last edited by Luthfi; October 10th, 2012 at 04:43 PM.
October 10th, 2012, 05:12 PM
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.
October 10th, 2012, 09:07 PM
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.
October 10th, 2012, 09:32 PM
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.
Originally Posted by sihaam
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.
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.
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.
Originally Posted by clieview
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.