如何使用vb.net仅允许AlphaNumeric字符在Windows应用程序中使用文本框 [英] How to allow only AlphaNumeric characters to textbox in windows application using vb.net

查看:70
本文介绍了如何使用vb.net仅允许AlphaNumeric字符在Windows应用程序中使用文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们,您好我在Windows应用程序中的vb.net中有一个需要。文本框只允许使用字母数字,第一个字母应该是字符而不是数字。不允许使用特殊字符,符号,下划线和空格。如果任何另一个字符应该输入然后提出验证消息。



请任何人帮助我。

解决方案

首先,请看我对这个问题的评论。



您可以轻松地从处理事件的输入中过滤掉不需要的字符 KeyPress 或覆盖虚拟方法 OnKeyPress

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onkeypress.aspx [ ^ ],

http:// msdn .microsoft.com / zh-cn / library / system.windows.forms.control.keypress.aspx [ ^ ]。



此事件已解除取消机制在上面的第二篇文章中;另请参阅: http://msdn.microsoft.com/en-us /library/system.windows.forms.keypresseventargs.handled.aspx [ ^ ]。



如果为属性值<$ c指定true $ c>处理的事件参数,事件的取消将导致忽略不需要的字符。现在,关于字母数字。您可以检查以下谓词:

http:// msdn .microsoft.com / zh-cn / library / system.char.isletter.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.char.isdigit.aspx [ ^ ]。



我希望你有这个想法。



还有一点:不要忘记允许退格。根据一些奇怪的历史原因,这个嘿被认为是一个字符,代码点#8。如果您使用其他不需要的字符过滤掉它,编辑控件(文本框或类似的东西)将不允许您删除以前的字符。



-SA


 ' 例如我允许。和,但如果你不需要那么就删除那些条件。 
功能 Isnumber( ByVal KCode As String As 布尔值
Isnumber = True
如果 IsNumeric(KCode) KCode<> ChrW(Keys.Back) KCode<> ChrW(Keys.Enter) KCode<> c KCode< > c 然后
Isnumber = False
MsgBox( 请仅输入数字,MsgBoxStyle.OkOnly)
结束 如果

结束 功能





 ' 将此代码放在textbox keypress事件下 
私有 Sub textbox1_KeyPress(sender As 对象,e 作为 System.Windows.Forms.KeyPressEventArgs)句柄 txtAmount.KeyPress
如果 Isnumber(e.KeyChar)那么
e.KeyChar =
结束 如果
结束 Sub


 ' 布尔标志,用于确定何时输入数字以外的字符。 
私有 nonNumberEntered 作为 布尔 = 错误

' 处理KeyDown事件以确定输入控件的字符类型。
< span class =code-keyw ord>私有 Sub textBox1_KeyDown( ByVal sender 正如 对象 ByVal e As System.Windows.Forms.KeyEventArgs)_
句柄 TextBox1.KeyDown
' 将标志初始化为false。
nonNumberEntered = False

' 确定击键是否是键盘顶部的数字。
< span class =code-keyword>如果 e.KeyCode< Keys.D0 OrElse e.KeyCode> Keys.D9 然后
' 确定是否按键是键盘上的数字。
如果 e.KeyCode< Keys.NumPad0 OrElse e.KeyCode> Keys.NumPad9 然后
' 确定是否键击是退格键。
如果 e.KeyCode<> Keys.Back 然后
' 非数字击键被按下了。
' 将标志设置为true并在KeyPress事件中进行评估。
nonNumberEntered = True
结束 如果
结束 如果
结束 如果
结束 Sub ' textBox1_KeyDown


' 此事件发生在KeyDown事件之后,可以使用
' 以防止字符进入控件。
私人 Sub textBox1_KeyPress( ByVal sender As 对象 ByVal e 作为 System.Windows.Forms.KeyPressEventArgs)_
句柄 TextBox1.KeyPress
' 检查KeyDown事件中设置的标志。
如果 nonNumberEntered = True 然后
' 阻止字符输入控件,因为它不是数字。
e.Handled = True
结束 如果
结束 Sub ' textBox1_KeyPress


Hi friends, Hi i have a requiment in vb.net in windows application. Textbox allows only alphanumerics and First letter should be a Characters only not a numbers. Doen't allow the special characters ,symbllos , underlines and spaces. If any of the another charactes should enter then raise the validation message.

Please any one help me.

解决方案

First of all, please see my comment to the question.

You can easily filter out unwanted characters from input handling the event KeyPress or overriding the virtual method OnKeyPress:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onkeypress.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress.aspx[^].

This event has the cancellation mechanism explained in the second article references above; see also: http://msdn.microsoft.com/en-us/library/system.windows.forms.keypresseventargs.handled.aspx[^].

If you assign true to the value of the property Handled of the event arguments, the cancellation of the event will cause unwanted character to be ignored. Now, about "alphanumeric". You can check the following predicates:
http://msdn.microsoft.com/en-us/library/system.char.isletter.aspx[^],
http://msdn.microsoft.com/en-us/library/system.char.isdigit.aspx[^].

I hope you got the idea.

And one important point: don't forget to allow backspace. By some weird historical reason, this hey is considered as a "character", with a code point #8. If you filter it out with other unwanted characters, edit controls (text boxes or anything like that) won't allow you to delete a previous character.

—SA


' for example i allow "." and "," but if you not need then just remove those condition.
Function Isnumber(ByVal KCode As String) As Boolean
        Isnumber = True
        If Not IsNumeric(KCode) And KCode <> ChrW(Keys.Back) And KCode <> ChrW(Keys.Enter) And KCode <> "."c And KCode <> ","c Then
            Isnumber = False
            MsgBox("Please Enter Numbers only", MsgBoxStyle.OkOnly)
        End If

    End Function



' put this code under textbox keypress event
  Private Sub textbox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtAmount.KeyPress
        If Not Isnumber(e.KeyChar) Then
            e.KeyChar = ""
        End If
    End Sub


' Boolean flag used to determine when a character other than a number is entered.
    Private nonNumberEntered As Boolean = False

    ' Handle the KeyDown event to determine the type of character entered into the control.
    Private Sub textBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) _
         Handles TextBox1.KeyDown
        ' Initialize the flag to false.
        nonNumberEntered = False

        ' Determine whether the keystroke is a number from the top of the keyboard.
        If e.KeyCode < Keys.D0 OrElse e.KeyCode > Keys.D9 Then
            ' Determine whether the keystroke is a number from the keypad.
            If e.KeyCode < Keys.NumPad0 OrElse e.KeyCode > Keys.NumPad9 Then
                ' Determine whether the keystroke is a backspace.
                If e.KeyCode <> Keys.Back Then
                    ' A non-numerical keystroke was pressed. 
                    ' Set the flag to true and evaluate in KeyPress event.
                    nonNumberEntered = True
                End If
            End If
        End If
    End Sub 'textBox1_KeyDown


    ' This event occurs after the KeyDown event and can be used 
    ' to prevent characters from entering the control.
    Private Sub textBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
        Handles TextBox1.KeyPress
        ' Check for the flag being set in the KeyDown event.
        If nonNumberEntered = True Then
            ' Stop the character from being entered into the control since it is non-numerical.
            e.Handled = True
        End If
    End Sub 'textBox1_KeyPress


这篇关于如何使用vb.net仅允许AlphaNumeric字符在Windows应用程序中使用文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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