限制文本框仅接受 10 位数字
我有一个 System::String^
文本框,我需要确认它只接受 10 位数字,不接受字母、符号等。我如何在 C++ Visual Studio 中实现它?我需要先将内容转换为 std::string
吗?
I have a Text Box that is a System::String^
, I need to confirm that this only accepts 10 digits numbers and no letters, symbols, etc. How would I implement this in C++ visual studio? Do I need to convert the contents to a std::string
first?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设这是一个 .NET winforms 文本框(因为您的代码片段是 C++/CLI),您需要设置“MaxLength”属性。 (类似于
TextBox^ tb = gcnew TextBox(); tb->MaxLength = 10
。)对于纯数字部分,您需要为 KeyDown 和 KeyPress 事件分配一个委托以确保输入的字符是数字。示例代码位于: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress(v=VS.71).aspx
Assuming this is a .NET winforms text box (since your snippet is C++/CLI), you want to set the "MaxLength" property. (Something like
TextBox^ tb = gcnew TextBox(); tb->MaxLength = 10
.)For the numbers-only part, you want to assign a delegate to the KeyDown and KeyPress events to make sure the entered character is a number. Sample Code is here: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress(v=VS.71).aspx