vb.net 在visual basic中只在文本框中输入数值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16154449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Enter numeric value only into textbox in visual basic
提问by user2308593
I am trying to set up a form to accept a telephone number, but i am unsure of how to validate it so it will only take numeric values with 11 digits.
我正在尝试设置一个表单来接受电话号码,但我不确定如何验证它,因此它只会采用 11 位数字值。
So far i have it working to ensure that there is something in the textbox
到目前为止,我已经在努力确保文本框中有内容
'Validate data for Telephone Number
If txtTelephoneNumber.Text = "" Then
txtTelephoneNumber.Focus()
MessageBox.Show("You must enter a Telephone Number.", "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Information)
回答by phadaphunk
I'll imply you are using Windows Forms.
Write this as your TextBox's Key Pressed event.
我会暗示您正在使用 Windows 窗体。
将此写为 TextBox 的 Key Pressed 事件。
Private Sub myTxtBox_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles myTxtBox.KeyPress
If txtTelephoneNumber.Text.Length > 11 Then
e.Handled= True
return
End If
If Asc(e.KeyChar) <> 8 Then
If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then
e.Handled = True
End If
End If
End Sub
This should(did not have time to test it) keep the user from inputing anything that is not a number. It should prevent him from inputting more than 11 numbers too.
这应该(没有时间对其进行测试)防止用户输入任何不是数字的内容。它也应该防止他输入超过 11 个数字。
回答by Fullyworked
In your textbox
keydown
event. Use the following:
在你的textbox
keydown
活动中。使用以下内容:
If Not IsNumeric(Chr(e.KeyCode)) Then
e.SuppressKeyPress = True
End If
If you want allow other characters you would do it like so:
如果您想允许其他字符,您可以这样做:
If Not IsNumeric(Chr(e.KeyCode)) And Not e.KeyCode = 8 And Not e.KeyCode = 46 Then
e.SuppressKeyPress = True
End If
'(8 = backspace key and 46 = Delete key)
回答by tymeJV
You can try
你可以试试
If txtTelephoneNumber.Text = "" Or Not IsNumeric(txtTelephoneNumber.Text) Or txtTelephoneNumber.Text.Length <> 11 Then
txtTelephoneNumber.Focus()
MessageBox.Show("You must enter a Telephone Number.", "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Information)
回答by APrough
Put this in your KeyPress event of the TextBox
把它放在你的 TextBox 的 KeyPress 事件中
'makes sure that only numbers and the backspace are allowed in the fields.
Dim allowedChars As String = "0123456789" & vbBack
'get a reference to the text box that fired this event
Dim tText As TextBox
tText = CType(sender, TextBox)
If allowedChars.IndexOf(e.KeyChar) = -1 Then
' Invalid Character
e.Handled = True
End If
if tText.Text.Length >= 11 then
e.Handled = True
End If
回答by Michael Parr
I use this for the keypress event
我将它用于按键事件
If e.KeyChar < CStr(0) Or e.KeyChar > CStr(9) Then e.Handled = True
Actually that looks different to what i remember using, but it works. Though you'll need to allow backspace too.
实际上这看起来与我记得使用的不同,但它有效。虽然你也需要允许退格。
Or i guess even shorter would be
或者我想更短的会是
If Not IsNumeric(e.KeyChar) Then e.Handled = True
also keypress event.
还有按键事件。
You can set the maximum length of a textbox using MaxLength
您可以使用 MaxLength 设置文本框的最大长度
回答by Jordan B
set max length to 12 put this in keypress of text1 and it will format it with dashes and only numbers
将最大长度设置为 12 将其放在 text1 的按键中,它将使用破折号和数字对其进行格式化
If Len(Text1.Text) = 3 Or Len(Text1.Text) = 7 Then
Text1.Text = Text1.Text & "-"
Text1.SelStart = Len(Text1.Text)
End If
If KeyAscii <> 127 And KeyAscii <> 8 And KeyAscii <> 13 Then
If IsNumeric(Chr(KeyAscii)) = False Then
KeyAscii = 0
End If
ElseIf KeyAscii = 8 Then
If Right(Text1.Text, 1) = "-" Then
Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1)
Text1.SelStart = Len(Text1.Text)
End If
End If
if you only want numeric text box use the following in the keypress
如果您只想要数字文本框,请在按键中使用以下内容
If KeyAscii <> 127 And KeyAscii <> 8 And KeyAscii <> 13 Then
If IsNumeric(Chr(KeyAscii)) = False Then
KeyAscii = 0
End If
End If
回答by user3700224
You can use the lostfocus event and in this event you can put something like this:
您可以使用 lostfocus 事件,在此事件中,您可以放置如下内容:
if not isnumeric(textbox1.text) then
textbox1.gotfocus 'this part is to validate only numbers in the texbox and not let
' him go from the texbox
endif
and you should define the maxlength for the textbox in just 11 characters
并且您应该仅用 11 个字符定义文本框的最大长度