我发现这个代码使我的文本框只接受数字.
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
您还需要处理粘贴的文本(可能没有按键).最好的方法是使用
MaskedTextBox.