Thanks to Handoko for your solution. It should work, but would be a lot of work for handling MaxValue and MinValue and the DecimalPlaces manually, because I need a couple of floating input fields. So I would prefer a solution which is nearer to TFloatSpinEdit, if there is any.
Yes, that's why I said that is not the best solution.
But actually it's not so hard. Just need to do these things:
1. Write a filter procedure
once and use it on all the TEdit.OnKeyPress events. You know, that is the 2 if conditions code that you've just tried previously.
2. Pay attention on the decimal symbol, do you want to use a dot or a comma or depend on the user regional setting on the computer. If you want to get the the symbol based on the setting on the user's computer, then this info is useful:
http://www.freepascal.org/docs-html/rtl/sysutils/decimalseparator.html3. Validate the TEdit.Text before you use it on your calculation. Pick one from these:
http://www.freepascal.org/docs-html/rtl/sysutils/strtofloat.htmlhttp://www.freepascal.org/docs-html/rtl/system/val.htmlShow an error message to user if the string can't be converted to a float variable.
4. If the string (TEdit.Text) can be converted to a float variable, check it again to make sure it is in the value range you want. Show an error message if the value is outside the range.
Note:
For step #3 and #4 can be combine into a single function, something like
function isValidValueInput(const S: string; var ResultVal: single; MinVal, MaxVal: single): Boolean;
It may seem difficult but once you've done it, you'll be glad you have learned something new. And you can save the code to reuse in the future. Challenge yourself to do something harder is one of the way to improve programming skill.