In Delphi, only decimals can be entered in the restricted text box (tedit ).

Source: Internet
Author: User
In some cases, you need to restrict only certain characters in the text box (the tedit control here) in the program. For example, you can only enter numbers. In this case, you can set the numbersonly attribute of tedit to achieve this. Here, tedit is limited to only 0 to 9 characters. If you want to restrict the number of decimal places, the decimal point is blocked. Therefore, the numbersonly attribute cannot be used, and no other attribute can be implemented. You have to do it yourself! Restrict input, which can be done in the onkeypress event. See the following code:Procedure tform1.edit1keypress (Sender: tobject; var key: Char); var EDT: tedit; STR: string; strl: string; strr: string; begin // get the current text content, note that the selected part should be removed (because it will be rewritten ). EDT: = tedit (sender); STR: = EDT. text; If length (EDT. seltext) <> 0 then begin strl: = leftstr (EDT. text, EDT. selstart); strr: = rightstr (EDT. text, length (EDT. text)-EDT. selstart-EDT. sellength); STR: = strl + strr; end; // restrict the input number/decimal point/backspace key if not charinset (Key, ['0 '.. '9 ','. ', #8]) then key: = #0; // not (key in ['0 '.. '9 ','. ', #8]) // the first digit cannot be the decimal point if (Key = '. ') and (EDT. selstart = 0) then key: = #0; // only one decimal point if (Key = '. ') and (Pos ('. ', STR)> 0) then key: = #0; // 0 cannot be inserted before (except when the first 0 is input) if (Key = '0 ') and (EDT. selstart = 0) and (STR <> '') then key: = #0; // you cannot enter 0 if (Key = '0') and (leftstr (STR, 1) = '0') and (Pos ('. ', STR) <= 0) then key: = #0; end;The above takes into account many cases, it is good to limit the input decimals. If the program has a lot of tedit to do this restriction, of course, you don't have to write code for each control, just specify the event to the same process.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.