C ++ Builder XE8上的TEdit输入验证 [英] TEdit Input Validation on C++ Builder XE8
问题描述
我是C ++ Builder XE8的新手。
I am very new to C++ Builder XE8.
我希望必须输入的最小和最大数字长度最多为六个数字,我也需要确保只输入数字(0为例外),而不输入字母字符,退格键,标点符号等。
I want the minimum and maximum length of numbers that must be typed is as much as six numbers, also I need to make sure that only number is entered (0 is exception), and not an alphabetic character, backspace, punctuation, etc.
我还要生成一个错误框如果输入了除数字以外的其他任何内容。
I would also like to produce an error box if anything other than a number is entered.
我尝试了几种代码组合,下面可以看到其中三种,但是这些代码都不起作用。
I've tried a few combinations of codes, three of which can be seen below, but none of those codes works.
任何帮助都将不胜感激!
Any help would sure be appreciated!
(1)。
void __fastcall TForm1::Edit1KeyPress(TObject *Sender, System::WideChar &Key)
{
Edit1->MaxLength = 6;
if (!((int)Key == 1-9)) {
ShowMessage("Please enter numerals only");
Key = 0;
}
}
(2)。
void __fastcall TForm1::Edit1KeyPress(TObject *Sender, System::WideChar &Key)
{
Edit1->MaxLength = 6;
if (Key <1 && Key >9) {
ShowMessage("Please enter numerals only");
Key = 0;
}
}
(3)。
void __fastcall TForm1::Edit1KeyPress(TObject *Sender, System::WideChar &Key)
{
Edit1->MaxLength = 6;
if( Key == VK_BACK )
return;
if( (Key >= 1) && (Key <= 9) )
{
if(Edit1->Text.Pos(1-9) != 1 )
ShowMessage("Please enter numerals only");
Key = 1;
return;
}
}
推荐答案
TEdit
具有 NumbersOnly
属性:
TEdit
has a NumbersOnly
property:
仅允许将数字键入文本编辑。
Allows only numbers to be typed into the text edit.
将其设置为true,然后让操作系统为您处理验证。但是,如果您要手动验证它,请使用以下命令:
Set it to true and let the OS handle the validation for you. But, if you want to validate it manually, use this:
void __fastcall TForm1::Edit1KeyPress(TObject *Sender, System::WideChar &Key)
{
// set this at design-time, or at least
// in the Form's constructor. It does not
// belong here...
//Edit1->MaxLength = 6;
if( Key == VK_BACK )
return;
if( (Key < L'0') || (Key > L'9') )
{
ShowMessage("Please enter numerals only");
Key = 0;
}
}
这篇关于C ++ Builder XE8上的TEdit输入验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!