Vb.net 如何将文本框输入过滤为仅数字?
如何抑制除数值以外的所有数据 这不适用于Vb.net 如何将文本框输入过滤为仅数字?,vb.net,Vb.net,如何抑制除数值以外的所有数据 这不适用于KeyDown(): 如果e.KeyDataKeys.D9,则 e、 已处理=真 如果结束 您可以选中Char.IsDigit(e.KeyChar),但在这种情况下,最好的做法是创建TextBox的子类并重写IsInputChar()。这样你就有了一个可重用的TextBox控件,你可以把它放到任何地方,这样你就不必重新实现逻辑了 (我的VB有点生锈了…) 我建议你使用正则表达式。你可以搜索谷歌,比如“正则表达式文本框仅限数字”,我想你会找到很多例子 例如,
KeyDown()
:
如果e.KeyDataKeys.D9,则
e、 已处理=真
如果结束
您可以选中Char.IsDigit(e.KeyChar),但在这种情况下,最好的做法是创建TextBox的子类并重写IsInputChar()。这样你就有了一个可重用的TextBox控件,你可以把它放到任何地方,这样你就不必重新实现逻辑了
(我的VB有点生锈了…)
我建议你使用正则表达式。你可以搜索谷歌,比如“正则表达式文本框仅限数字”,我想你会找到很多例子 例如,如果您在ASP.NET中,则可以按如下方式进行操作:
<asp:TextBox
ID="txtPhoneNumber"
runat="server"
Text='<%#Bind("phoneNumber") %>'
MaxLength="15">
</asp:TextBox>
<asp:RegularExpressionValidator
ID="rfvUSerPhoneNumberValidate"
runat="server"
ControlToValidate="txtPhoneNumber"
Display="Dynamic"
ValidationExpression="^[0-9]{1,15}$"
ErrorMessage="Please enter only numeric value for Phone Number"
EnableViewState="true">
</asp:RegularExpressionValidator>
有很多方法可以做到这一点。我很快就尝试了一下,然后就这样做了。我使用了文本框的按键子功能,并将每个按键传递给IsNumber函数 注意:我允许使用退格键,以防您在数字上出错并希望删除 取出如果e.KeyChar ChrW(Keys.Back),如果不需要退格,则取出/End If部分
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar <> ChrW(Keys.Back) Then
If Char.IsNumber(e.KeyChar) Then
Else
e.Handled = True
End If
End If
End Sub
Private Sub TextBox1\u KeyPress(ByVal sender作为对象,ByVal e作为System.Windows.Forms.KeyPressEventArgs)处理TextBox1.KeyPress
如果e.KeyChar ChrW(key.Back)则
如果Char.IsNumber(例如KeyChar),那么
其他的
e、 已处理=真
如果结束
如果结束
端接头
此代码将帮助您限制多个文本框仅接受数值和退格键。但是,当您不想接受backspace键时,可以从代码中删除If e.KeyChar ChrW(Keys.Back)和End If值。此线程中kevchadders解决方案的增强版本
Private Sub TextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress, TextBox2.KeyPress, TextBox3.KeyPress
If e.KeyChar <> ChrW(Keys.Back) Then
If Char.IsNumber(e.KeyChar) Then
Else
e.Handled = True
End If
End If
End Sub
Private Sub TextBox_KeyPress(ByVal sender作为对象,ByVal e作为System.Windows.Forms.KeyPressEventArgs)处理TextBox1.KeyPress、TextBox2.KeyPress、TextBox3.KeyPress
如果e.KeyChar ChrW(key.Back)则
如果Char.IsNumber(例如KeyChar),那么
其他的
e、 已处理=真
如果结束
如果结束
端接头
这将允许数字输入、退格以更正输入,以及小数点
If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso e.KeyChar <> ControlChars.Back AndAlso e.KeyChar <> ControlChars.Cr AndAlso e.KeyChar <> "." Then
Beep()
e.Handled = True
End If
如果(e.KeyChar<“0”或e.KeyChar>“9”)和e.KeyChar ControlChars.Back以及e.KeyChar ControlChars.Cr和e.KeyChar“
哔()
e、 已处理=真
如果结束
这是限制文本框中数字输入的另一种方法。使用按键事件
如果Asc(e.KeyChar)13和Asc(e.KeyChar)8和Asc不是数字(e.KeyChar),则
MessageBox.Show(“仅限数字”)
e、 已处理=真
如果结束
端接头
希望有帮助!thnks 将帮助您
Public Function IsNumericTextbox(ByVal sender As TextBox, ByVal KeyChar As Char) As Boolean
'set TRUE: cause a exception when the keychar is not Allowed into vars: allowedChars, allowedOneChar, allowedExceptionChar
Dim UseThrowDebuggy As Boolean = False
Dim allowedChars As String = "0123456789"
Dim allowedOnceChar As Char() = {"."}
Dim allowedExceptionChar As Keys() = {Keys.Back}
Dim idxAllowedNotFound As Integer
Dim idxCountOne As Integer = 0
idxAllowedNotFound = allowedChars.IndexOf(KeyChar)
If idxAllowedNotFound = True Then
'AllowedOnce
For Each _c As Char In allowedOnceChar
If _c = KeyChar Then
'Count Check
For Each _cc As Char In sender.Text
If _c = _cc Then idxCountOne += 1
Next
If idxCountOne = 0 Then
Return False
Else
Return True
End If
End If
Next
'Exceptions
For i As Integer = 0 To allowedExceptionChar.Count - 1
If Asc(KeyChar) = Convert.ToUInt32(allowedExceptionChar(i)) Then Return False
Next
'Not Throw
If UseThrowDebuggy = False Then
If Char.IsNumber(KeyChar) Then
Return False
Else
Return True
End If
End If
'Outside to end for throw
Else
'AllowedChars
Return False
End If
Dim _kc As String = ControlChars.NewLine & "Char: " & KeyChar & ControlChars.NewLine & "Asc: " & Asc(KeyChar) & ControlChars.NewLine
Throw New Exception("UseThrowDebuggy found a unknow KeyChar: " & _kc)
End Function
要使用我的功能,请将此代码添加到文本框中\u按键:
e、 Handled=IsNumericTextbox(发送方,例如KeyChar)
功能的目的可能有助于提供其他解决方案。在每个按键上检查一个数字值可能有点过头了。然后,您必须考虑退格、删除、复制、粘贴等,从而使其更具杀伤力
例如,如果要存储电话号码,则应在验证和更新步骤中使用“IsNumeric”功能。或者,如果您选择项目数量,“NumericUpDown”控件比文本框更合适。我想您可以在这里找到答案:(请注意Jeff Yates的评论)不要忘记允许(本地化)千位和小数点分隔符这能处理从剪贴板粘贴文本的情况吗?删除键呢?从剪贴板剪切粘贴怎么样?当然,它们可以添加到IF语句中忽略。说得好。
If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso e.KeyChar <> ControlChars.Back AndAlso e.KeyChar <> ControlChars.Cr AndAlso e.KeyChar <> "." Then
Beep()
e.Handled = True
End If
Public Function IsNumericTextbox(ByVal sender As TextBox, ByVal KeyChar As Char) As Boolean
'set TRUE: cause a exception when the keychar is not Allowed into vars: allowedChars, allowedOneChar, allowedExceptionChar
Dim UseThrowDebuggy As Boolean = False
Dim allowedChars As String = "0123456789"
Dim allowedOnceChar As Char() = {"."}
Dim allowedExceptionChar As Keys() = {Keys.Back}
Dim idxAllowedNotFound As Integer
Dim idxCountOne As Integer = 0
idxAllowedNotFound = allowedChars.IndexOf(KeyChar)
If idxAllowedNotFound = True Then
'AllowedOnce
For Each _c As Char In allowedOnceChar
If _c = KeyChar Then
'Count Check
For Each _cc As Char In sender.Text
If _c = _cc Then idxCountOne += 1
Next
If idxCountOne = 0 Then
Return False
Else
Return True
End If
End If
Next
'Exceptions
For i As Integer = 0 To allowedExceptionChar.Count - 1
If Asc(KeyChar) = Convert.ToUInt32(allowedExceptionChar(i)) Then Return False
Next
'Not Throw
If UseThrowDebuggy = False Then
If Char.IsNumber(KeyChar) Then
Return False
Else
Return True
End If
End If
'Outside to end for throw
Else
'AllowedChars
Return False
End If
Dim _kc As String = ControlChars.NewLine & "Char: " & KeyChar & ControlChars.NewLine & "Asc: " & Asc(KeyChar) & ControlChars.NewLine
Throw New Exception("UseThrowDebuggy found a unknow KeyChar: " & _kc)
End Function
Public Class NumericTextBox : Inherits System.Windows.Forms.TextBox
Protected Overrides Sub OnKeyPress(e As Windows.Forms.KeyPressEventArgs)
If Char.IsDigit(e.KeyChar) Or
Char.IsControl(e.KeyChar) Or
e.KeyChar = lobalization.CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator Then
MyBase.OnKeyPress(e)
Else
e.Handled = True
End If
End Sub
End Class