Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
TEdit在C+上的输入验证+;建筑商XE8 我对C++ Builder XE8非常新。_C++_C++builder_C++builder Xe8 - Fatal编程技术网

TEdit在C+上的输入验证+;建筑商XE8 我对C++ Builder XE8非常新。

TEdit在C+上的输入验证+;建筑商XE8 我对C++ Builder XE8非常新。,c++,c++builder,c++builder-xe8,C++,C++builder,C++builder Xe8,我希望必须输入的数字的最小和最大长度都是六个数字,而且我需要确保只输入数字(0是例外),而不是字母字符、退格、标点符号等 如果输入的不是数字,我还想生成一个错误框 我尝试了几种代码组合,下面可以看到其中三种,但这些代码都不起作用 任何帮助都将不胜感激 (1) (2) void\uu fastcall TForm1::Edit1KeyPress(TObject*发送方,System::WideChar&Key) { Edit1->MaxLength=6; 如果(图例9){ ShowMessag

我希望必须输入的数字的最小和最大长度都是六个数字,而且我需要确保只输入数字(0是例外),而不是字母字符、退格、标点符号等

如果输入的不是数字,我还想生成一个错误框

我尝试了几种代码组合,下面可以看到其中三种,但这些代码都不起作用

任何帮助都将不胜感激

(1)

(2)

void\uu fastcall TForm1::Edit1KeyPress(TObject*发送方,System::WideChar&Key)
{
Edit1->MaxLength=6;
如果(图例9){
ShowMessage(“请仅输入数字”);
键=0;
}
}
(3)

void\uu fastcall TForm1::Edit1KeyPress(TObject*发送方,System::WideChar&Key)
{
Edit1->MaxLength=6;
if(Key==VK_BACK)
返回;
如果((键>=1)和&(键文本位置(1-9)!=1)
ShowMessage(“请仅输入数字”);
键=1;
返回;
}
}

签出
TMaskEdit

TMaskEdit是一个特殊的编辑控件,它根据一个掩码验证输入的文本,该掩码编码文本可以采用的有效形式。该掩码还可以格式化显示给用户的文本

编辑:设置最小长度

void __fastcall TForm1::MaskEdit1Exit(TObject *Sender)
{
   if (MaskEdit1->Text.length() < 6)
   {
     //your error message, or throw an exception.
   }
}
void\uu fastcall TForm1::MaskEdit1Exit(TObject*Sender)
{
if(MaskEdit1->Text.length()<6)
{
//您的错误消息,或引发异常。
}
}
TEdit
有一个属性:

只允许在文本编辑中键入数字

将其设置为true并让操作系统为您处理验证。但是,如果您希望手动验证,请使用以下方法:

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;
    }
}
void\uu fastcall TForm1::Edit1KeyPress(TObject*发送方,System::WideChar&Key)
{
//在设计时设置,或至少在设计时设置
//在窗体的构造函数中。它不
//属于这里。。。
//Edit1->MaxLength=6;
if(Key==VK_BACK)
返回;
如果((键L'9'))
{
ShowMessage(“请仅输入数字”);
键=0;
}
}

谢谢,但您能提供一种方法让我输入至少6个数字的输入长度吗?@Romeo设置
MaxLength
属性以将输入限制为6个字符。如果您想检查最小长度,请在
OnExit
事件中执行。您可以在TEdit或TMaskEdit控件中执行此操作。但我的意思是当输入少于六个数字时,它将产生一个错误。如何在
OnExit
事件中做到这一点?@Romeo看看我的更新答案。如果您阅读一些文档,所有这些都很容易理解。谢谢您的帮助。但是,您能重新阅读我上面的问题吗?因为尽管您的代码是有效的但仍然没有防止输入长度至少为6numbers@Romeo您无法验证按键事件的最小长度。您如何知道用户是否将继续写入?正确的解决方案是在控件失去焦点或准备使用值时验证输入值es(例如,单击按钮)。在那里,您可以根据需要验证长度和内容。@Romeo:正如我在代码示例中提到的,您仍然可以设置
MaxLength
值,只是不要在
OnKeyPress
事件中进行设置。@Rodrigoómez:如果在编辑失去焦点时进行验证,您必须小心。在触发
OnExit
事件时,input focus已经在移动到另一个窗口的过程中。如果您做了任何事情使另一个窗口接收到焦点,例如显示弹出消息,您可能会将Windows/VCL窗口管理搞得一团糟,导致UI停止正常工作。例如,单击按钮进行验证是可以的,而且是首选的。@Rodrigo:your'r是的。目的是定期强制用户输入最小设置长度6。
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;
  }
}
void __fastcall TForm1::MaskEdit1Exit(TObject *Sender)
{
   if (MaskEdit1->Text.length() < 6)
   {
     //your error message, or throw an exception.
   }
}
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;
    }
}