Only accept digits for textbox

前端 未结 8 958
小鲜肉
小鲜肉 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:48

    Use this code, it will help you

    Public Function OnlyDigitsOnKeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
    
    Try
    
        If System.Char.IsDigit(e.KeyChar) = False And e.KeyChar <> Microsoft.VisualBasic.Chr(8)        And e.KeyChar <> Microsoft.VisualBasic.Chr(46) Or (InStr(sender.text, ".") > 0 And  e.KeyChar = Microsoft.VisualBasic.Chr(46)) 
        Then
                    e.Handled = True
        End If
            Catch ex As Exception
                Common.ErrorHandler(ex)
            End Try
    End Function
    

提交回复
热议问题