本文介绍了限制在Vb中使用文本框控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望该用户在文本框中仅输入数字而不是字母.我如何才能检查用户是否以编程方式仅在文本框中输入数字.或如何阻止用户在文本框中输入字母.感谢您的帮助.
I want that user should enter only numbers in the text box not alphabets. how can i check that user enter only number in the textbox programatically. or how can I stop users to enter alphabets in the text box. I will appreciate your help.
推荐答案由于您已将其标记为VB.NET,因此我假设您正在使用Winforms. 现在,您只需要将OnKeyDown事件绑定到文本框即可.在后面的代码中,在运行时,每当按下一个键时,就会调用OnKeyDown方法.在其中,检查按键,然后相应地决定是否要允许它. 如果需要,请在此处 [ ^ ]. Since you have tagged it as VB.NET, I assume you are taking of Winforms. Now, all you need is to tie a OnKeyDown event to the textbox. In code behind, at runtime, whenever a key is pressed, OnKeyDown method would be invoked. In it, check for the key stroke and accordingly decide if you want to allow it or not. If needed, have a look at similar discussion here[^].
更好的想法是您可以尝试进行用户控制.因为如果在onkeydown中验证了think(仅数字)(这也是错误的,请使用onkeypress),那么您必须在每个文本框按键中编写该验证代码.但是您将为数字文本框创建用户控件 那么您可以在您的项目中使用任何软件.这只是很好.而且也很容易. 只需添加一个新项,选择用户控件并拖动一个文本框,然后编写此代码msdn.microsoft/zh-cn/library/ms229644.aspx.最终构建您的项目,此文本框将出现在工具箱中.第一次使用和享受 Better think is u can try to make a usercontrol. because if validate that think(only numeric) in onkeydown( this is also wrong, use onkeypress) then you must write that validation code in every textbox keypress. But you will make a user control for numeric textbox then u can use any ware in your project. this is only good. and its easy also. just add a new item, choose user control and drag a text box then write this code msdn.microsoft/en-us/library/ms229644.aspx . finally build your project, this textbox will come on toolbox. jst use and enjoy
更多推荐
限制在Vb.net中使用文本框控件
发布评论