C++ Builder XE8 上的 TEdit 输入验证
TEdit Input Validation on C++ Builder XE8
我是 C++ Builder XE8 的新手。
我希望必须输入的数字的最小和最大长度是六个数字,我还需要确保只输入数字(0 除外),而不是字母字符、退格键、标点等
如果输入的不是数字,我还想生成一个错误框。
我已经尝试了一些代码组合,下面可以看到其中的三个,但是其中 none 个代码有效。
我们将不胜感激!
(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;
}
}
TMaskEdit is a special edit control that validates the text entered against a mask that encodes the valid forms the text can take. The mask can also format the text that is displayed to the user.
编辑:设置最小长度
void __fastcall TForm1::MaskEdit1Exit(TObject *Sender)
{
if (MaskEdit1->Text.length() < 6)
{
//your error message, or throw an exception.
}
}
TEdit
有一个 NumbersOnly
属性:
Allows only numbers to be typed into the text edit.
将其设置为 true 并让 OS 为您处理验证。但是,如果您想手动验证它,请使用:
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 的新手。
我希望必须输入的数字的最小和最大长度是六个数字,我还需要确保只输入数字(0 除外),而不是字母字符、退格键、标点等
如果输入的不是数字,我还想生成一个错误框。
我已经尝试了一些代码组合,下面可以看到其中的三个,但是其中 none 个代码有效。
我们将不胜感激!
(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;
}
}
TMaskEdit is a special edit control that validates the text entered against a mask that encodes the valid forms the text can take. The mask can also format the text that is displayed to the user.
编辑:设置最小长度
void __fastcall TForm1::MaskEdit1Exit(TObject *Sender)
{
if (MaskEdit1->Text.length() < 6)
{
//your error message, or throw an exception.
}
}
TEdit
有一个 NumbersOnly
属性:
Allows only numbers to be typed into the text edit.
将其设置为 true 并让 OS 为您处理验证。但是,如果您想手动验证它,请使用:
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;
}
}