Unified validation of the Delphi text,combox input Box

Source: Internet
Author: User

To prevent the user from mistakenly entering is a necessary work in software development, besides, it also provides the maximum convenience for the user's use. Of course, we can take advantage of or develop new components to accomplish these functions. However, in team development, each member to develop their own modules with the components they think are good, which will cause trouble to the later maintenance of the software. At the time of completion, the project leader will not buy your account. If you use function calls to accomplish these functions, the old cap is not the same. The following is a restricted user input function for commonly used Delphi components.
(i) input of Tedit, Tdbedit, Tcombobox, Tdbcombobox
There are three types of restrictions:
(1) any input
(2) Integer input
(3) floating point input
The following items are restricted:
(1) Integer input can only enter number 0-9, +,-
(2) floating point input can only enter the number 0-9, + 、-、.
(3) + and-only one, and only the front
(4). There can only be one
(5) Limit the number of decimal digits
The functions are as follows:

Procedure Mxformatkeypress (text:string;                           Selstart,sellength:integer; var Key:char; Edittype:integer; Digits:integer) begin{--------------------------------------------filter Character--------------------------} {esc/  Backspace key or any input when exiting} if (key= #27) or (key= #8) or (edittype= 1) then exit; If edittype= 2 Then if not (Key in [' 0 ' ... ')       9 ', ' + ', '-'] then begin key:= #0;     Exit;  End If edittype= 3 then if not (Key in [' 0 ' ... ') 9 ', ' + ', '-', '. '])       Then begin key:= #0;     Exit; End  {--------------------------------------------control +----------------------------}  {-+-can only be in the first position} {1, exists 2, does not exist 3, select 4, do not select} if (key = '-') or (key = ' + ') THEN BEGIN if (POS ('-', text) > 0) or (POS (' + ', text) & Gt    0) and (sellength= 0) then key:= #0;  If SelStart > 0 then key:= #0; End  {------------------------------------------------------------------------------}  {Control.} if (Key = '. ') and (edittype=3) then BEGIN if (Pos ('. ', Text) > 0) and (POS ('. ', text) < SelStart) or (POS ('. ', text) > (selstart+sellength))) then key:= #0;  If SelStart = 0 then key:= #0; End {------------------------------------------------------------------------------}//Verify the decimal position if (digits>0) and (        Selstart+sellength > 0) and (edittype=3) then if (POS ('. ', text) >0) and (Selstart>=pos ('. ", text)) then If length (text)-pos ('. ', text) >=digits then key:= #0; end;

This function is called in the onkeypress event of the restricted component. Key is the Key:char parameter that onkeypress carries, EditType is the type of restriction: 1-arbitrary input, 2-integer input, 3-floating-point input, the number of decimal places for floating-point numbers, and if it is zero, you can enter any number of digits. In addition, this function is only applicable to derived classes of Twincontrol classes with properties such as text, SelStart, SelLength, and so on.

The two-level function that restricts each component is as follows:

Limit Tedit, Tdbedit:
Procedure mxformateditkeypress (Edit:tcustomedit;var Key:char; Edittype:integer;
Digits:integer);
Begin
Mxformatkeypress (edit.text,edit.selstart,edit.sellength,key,edittype,digits);
End

Limit Tcombobox:
Procedure mxformatcombokeypress (Combo:tcombobox;var Key:char; Edittype:integer;
Digits:integer);
Begin
Mxformatkeypress (combo.text,combo.selstart,combo.sellength,key,edittype,digits);
End

Unified validation of the Delphi text,combox input Box

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.