只接受文本框的数字 [英] Only accept digits for textbox

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

问题描述

我发现这段代码可以让我的文本框只接受数字.

I found this code for making my textbox only accept numbers.

Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    Dim allowedChars As String = "0123456789"
    If allowedChars.IndexOf(e.KeyChar) = -1 Then
        ' Invalid Character
        e.Handled = True
    End If
End Sub

但是...用户无法使用退格按钮删除数字.那我该怎么办?

But... the user can't delete the numbers using the backspace button. How do I do then?

推荐答案

您还需要处理粘贴的文本(可能没有按键).最好的方法是使用 MaskedTextBox.

You also need to handle pasted text (there may not be a keypress). The best way to do this is with a MaskedTextBox.

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

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