Only accept digits for textbox

前端 未结 8 967
小鲜肉
小鲜肉 2020-12-10 16:13

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.Key         


        
8条回答
  •  佛祖请我去吃肉
    2020-12-10 16:51

     Private Sub txtValue_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles                 txtValue.KeyPress
            'Dim allowedChars As String = "0123456789"
            'If allowedChars.IndexOf(e.KeyChar) = -1 Then
            '    ' Invalid Character
            '    e.Handled = True
            'End If
            'If (e.KeyChar = Microsoft.VisualBasic.Chr(8)) Then
            '    e.Handled = True
            'End If
            If Char.IsDigit(e.KeyChar) = False And Char.IsControl(e.KeyChar) = False Then
                e.Handled = True
            End If
        End Sub                                
    

提交回复
热议问题