限制文字框 [英] restricting text box

查看:104
本文介绍了限制文字框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我可以寻求帮助吗?

如何使用VB.net代码限制用户仅在文本框中输入字符值.



干杯

Srikar

Hi,

may i request for help

How to restrict a user to enter only character values in a text box using VB.net code.



Cheers

Srikar

推荐答案

我得到的是要防止用户在文本框中输入任何数字,对吗?
如果是的话
您没有提到要在Windows或Web应用程序上执行此操作

对于Windows应用程序,您可以将texbox类继承到一个新类,例如CustomTextbox
覆盖keydown事件(以防止在用户输入时发生此事件)
或覆盖已更改的文本(在用户输入文本后验证文本),这取决于您选择写入事件.在第一种情况下,例如:
What i got you want to prevent the user to enter any numbers inside the textbox , am i right ?
If yes
You didn''t mention you want to do this on windows or web application

for windows application you can inherit the texbox class to a new one for example called CustomTextbox
override the the keydown event (to prevent it while the user is entering )
or override the textchanged (to validate the text after the user enter the text ) , it''s up to you to choose the write event. In the first case Ex:
public class CustomTextbox : TextBox
   {
       protected override void OnKeyDown(KeyEventArgs e)
       {
           base.OnKeyDown(e);
           //write your own logic here
       }
   }


在e内部,您会发现太多数据无法验证用户输入的新字符,如果无效,则可以对其进行转义

这将帮助您稍后使此文本框多功能控件添加一些新属性,例如验证类型(仅数字或字符).

因此希望我能为您提供愉快的编程帮助


Inside the e you will find too much data to validate the new character entered by the user and you can escape it if it''s not valid

this will help you later on to make this textbox multi function control throw adding some new properties like the validation type (numeric or characters only)

So hope that i could help , happy programming


在使用Web的情况下,我认为您可以使用javascript处理此问题:
In case of Web I think you might use javascript to handle this :

function validateNumericOnKeyPress(e) {
        var evt = e || window.event;
        var key = evt.keyCode || evt.which;
        var src = evt.srcElement;

        if ((key >= 48 && key <= 57) || key == 8 || key == null)
            return true;
        else if (src.value.indexOf(".") == -1 && key == 46)
            return true;
        else
            return false;
}



将其放置在头部的脚本块中.

现在在txtbox中使用



Place this inside a script block on head section.

now in the txtbox use

<br />
txtbox.Attributes.Add("onkeypress", "javascript:return validateNumericOnKeyPress(event)")



对于Windows,我认为答案已经提到.刚刚添加到其中,您需要在文本框上跟踪按键事件并使用
e.Handled = True /False ...

e.Handled = True时,字符将不会出现在文本框中.

希望这对您有所帮助. :thumbsup:



For windows, I think the answer is already mentioned. Just added to that you need to track keypress event on the textbox and use
e.Handled = True /False ...

when e.Handled = True the character will not appear in the textbox.

Hope this help you. :thumbsup:


这篇关于限制文字框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆