In delphi, only numbers can be entered for editing control.

Source: Internet
Author: User

How to Control edit in Delphi? Only numbers can be input
When there are too many threads, there are too many threads, too many threads.

If not (key in ['0'... '9', #8]) then key: = #0;

You can only enter Chinese characters, but not numbers or other characters.
When there are too many threads, there are too many threads, too many threads.
In the onkeypress event of edit
Procedure tform1.edit1keypress (Sender: tobject; var key: Char );
Begin
If (ord (key) <127) or (ord (edit1.text [1])> 127) then
Key: = #0;
End;

You can enter only numbers in a text box, and modify the number with the backspace key when the entered number is incorrect.
When there are too many threads, there are too many threads, too many threads.
Because the ASCII value of backspace is 8, you can do as follows:
If (Key <> #46) and (Key <#48) or (Key> #57) and (Key <> #8) then
// If the input is not a number or decimal point (#46 indicates the decimal point)
Begin
Key: = #0; // cancel the input content (#0 indicates a null value)
Showmessage ('input error! Enter a number! '); // Send a prompt
End;

Method 2:
If not (key in ['0' .. '9', #13, #8, #46]) then
Key: = #0;
In this way, you can

Only numbers are allowed, but other characters are not allowed.
When there are too many threads, there are too many threads, too many threads.
Edit attribute Maxlength setting 2;
In onkeypress of edit
Procedure Tmainform. editkeypress (sender: tobject; var key: char );
Var
Uflag: integer;
Begin
Uflag: = Tedit (sender). Tag;
If (not (key in ['1' .. '9']) and (not (key = #8) then key: = #0;
End;

Method 2:
Set maxlength of edit to 2;
In the edit onkeypress event
Procedure Ttbdlform. Edit1KeyPress (Sender: TObject; var Key: Char );
Begin
If key <> #9 then // #9 is a space. You can find the value of the Number 1 \ 2 \ 3.
Showmessage ('Enter the number ')
End;

Only numbers and decimal places can be entered
When there are too many threads, there are too many threads, too many threads.
Procedure TForm1.Edit1KeyPress (Sender: TObject; var Key: Char );
Begin
If not (key in ['0'... '9', '.', #8, #32]) then
Key: = #0;
End;
End.
However, if you want to enter only numbers and the format is limited, you 'd better use a third-party control'

Method 2:
You can add the following code in keypress, enter a number, and use the backspace to delete the number. You can use the press ENTER
Procedure TForm1.Edit1KeyPress (Sender: TObject; var Key: Char );
Begin
Case Key
'0'... '9', #8, #13, #27 ,'.':;
Else
Begin
MessageBox (Handle, 'Enter the number', PChar ('input error'), MB_ OK + MB_ICONINFORMATION );
Key: = #0;
End;
End;
End;

 

 

 

Procedure TForm1.Edit1KeyPress (Sender: TObject; var Key: Char );
Begin
If not (Key in ['0'... '9', #8, #13]) then
Begin
Key: = #0;
ShowMessage ('only numbers allowed ');
End;

End;

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.