vb.net 允许数字、点和退格,并在文本框中删除
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11253514/
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
allow numbers, dots and backspace and delete in textbox
提问by Christoph
i have the following code:
我有以下代码:
Private Sub TxtPStof_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TxtPStof.KeyPress
e.Handled = Not (Char.IsDigit(e.KeyChar) Or e.KeyChar = ".")
End Sub
which allows only digits and . in my textbox, however i also need to be able to delete values using the backspace or the delete button.
它只允许数字和 . 在我的文本框中,但是我还需要能够使用退格键或删除按钮删除值。
Is this possible?
这可能吗?
Thanks! :)
谢谢!:)
回答by Konrad Rudolph
This is the wrong approach.
这是错误的做法。
It's universally agreed that restricting the user's input is bad for the user experience, and you will invariably fail to handle special cases (what about Ctrl+V, for instance? Ah, you forgot about that. Everybody does).
普遍认为限制用户输入对用户体验不利,并且您总是无法处理特殊情况(例如Ctrl+V呢?啊,您忘记了。每个人都这样做)。
Instead, .NET offers the Validating
eventfor validating user input. You should intercept thatevent, notthe keypress. Doallow users to enter text however they want; in particular, allow them to make mistakes (e.g. mistyping) without interruption –?that would be extremely disruptive and nothelpful.
相反,.NET 提供用于验证用户输入的Validating
事件。您应该拦截该事件,而不是按键。不要让用户然而他们想输入文字; 特别是,允许他们不中断地犯错误(例如打错字)——这将是非常具有破坏性的并且没有帮助。
Then, once they're finished (because input focus leaves the control), do an input validation in one go.
然后,一旦它们完成(因为输入焦点离开控件),一次性进行输入验证。
回答by Steve
While I totally agree with the answer from Konrad Rudolph
(It's really a messy affair to handle the user input in the KeyPress event)
I wish to give an answer at your question.
虽然我完全同意 Konrad Rudolph 的回答
(处理 KeyPress 事件中的用户输入真的很麻烦),但
我想回答你的问题。
The MSDN in the KeyPressdocs states that The KeyPress event is not raised by noncharacter keys
. This means that you don't get the Delete key, but only the BackSpace key. You could handle this situation with this little change to your event handler
KeyPress文档中的 MSDN指出The KeyPress event is not raised by noncharacter keys
. 这意味着您无法获得 Delete 键,而只能获得 BackSpace 键。你可以通过对事件处理程序的这个小改动来处理这种情况
Private Sub TxtPStof_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TxtPStof.KeyPress
if e.KeyChar <> ControlChars.Back then
e.Handled = Not (Char.IsDigit(e.KeyChar) Or e.KeyChar = ".")
end if
End Sub
回答by Pankti
If Char.IsLetterOrDigit(e.KeyChar) Or e.KeyChar = ControlChars.Back Or e.KeyChar = "." Then
e.Handled = False
Else
e.Handled = True
End If
回答by slek
this code works fine for me.
这段代码对我来说很好用。
If Not ((Asc(e.KeyChar) >= 48 And Asc(e.KeyChar) <= 57) Or Asc(e.KeyChar) = 46 Or Asc(e.KeyChar) = 8 Or Asc(e.KeyChar) = 127) Then
e.KeyChar = ""
e.Handled = False
End If
回答by ilan
try this one:
试试这个:
If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then
If Not (Asc(e.KeyChar) = 8 Or Asc(e.KeyChar) = 46) Then e.Handled = True
End If
回答by bloodfeast
Private Sub textbox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles textbox.KeyPress
If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso e.KeyChar <> ControlChars.Back Then
e.Handled = True
End If
End Sub
This should do what you want it to do.
这应该做你想要它做的事情。
回答by Edward Nazareno
Put this on keypress event. This allows dots and numbers only
把它放在按键事件上。这仅允许点和数字
If Asc(e.KeyChar) <> 8 Then
If Asc(e.KeyChar) < 46 Or Asc(e.KeyChar) > 57 Or (Asc(e.KeyChar) < 48 And Asc(e.KeyChar) > 46) Then
e.Handled = True
End If
End If